• MyBatis的使用增删改查(两种分页查询)


    文件目录

    写一下每个文件的代码

     UserDao.java

    package cn.zys.dao;
    
    import java.io.IOException;
    import java.util.List;
    
    import org.apache.ibatis.session.SqlSession;
    
    import cn.zys.entity.User;
    import cn.zys.until.MyBatisUtils;
    
    public class UserDao {
        //根据id查用户
        public User getById(int id) throws IOException{
            SqlSession sqlsession = MyBatisUtils.getSqlSession();
            User user = sqlsession.selectOne("cn.zys.entity.UserMapper.selectUser", id);
            /*System.out.println(user.toString());*/
            sqlsession.close();
            return user;
        }
        
        //查询所有用户
        public List<User> selectAll() throws IOException{
            SqlSession sqlsession = MyBatisUtils.getSqlSession();
            List<User> list = sqlsession.selectList("cn.zys.entity.UserMapper.selectAll");
            sqlsession.close();
            return list;
        }
        
        //添加用户
        public int addUser(User user) throws IOException{
            SqlSession sqlsession = MyBatisUtils.getSqlSession();
            int res = sqlsession.insert("cn.zys.entity.UserMapper.insertUser",user);
            sqlsession.commit();
            sqlsession.close();
            return res;
        }
        
        //更新用户
        public int updateUser(User user) throws IOException{
            SqlSession sqlsession = MyBatisUtils.getSqlSession();
            int res = sqlsession.update("cn.zys.entity.UserMapper.updateUser",user);
            sqlsession.commit();
            sqlsession.close();
            return res;
        }
        
        //删除用户
        public int deleteUser(int id) throws IOException{
            SqlSession sqlsession = MyBatisUtils.getSqlSession();
            int res = sqlsession.delete("cn.zys.entity.UserMapper.deleteUser",id);
            sqlsession.commit();
            sqlsession.close();
            return res;
        }    
    //分页查询
        public List<User> selectLimt(int startindex,int pagenum) throws IOException{
            SqlSession sqlsession = MyBatisUtils.getSqlSession();
            Map<String,Integer> maps = new HashMap<String, Integer>();
            maps.put("startindex", (startindex-1)*pagenum);
            maps.put("pagenum", pagenum);
            List<User> list = sqlsession.selectList("cn.zys.entity.UserMapper.selectLimit",maps);
            sqlsession.commit();
            sqlsession.close();
            return list;
        }
        
        //rowbangs分页查询
        public List<User> selectLimts(int startindex,int pagenum) throws IOException{
            SqlSession sqlsession = MyBatisUtils.getSqlSession();
            RowBounds rowbounds = new RowBounds((startindex-1)*pagenum,pagenum);
            List<User> list = sqlsession.selectList("cn.zys.entity.UserMapper.selectLimits",null,rowbounds);
            sqlsession.commit();
            sqlsession.close();
            return list;
        }
    }

    User.java

    package cn.zys.entity;
    
    public class User {
        private int id;
        private String name;
        private String pwd;
        //
        public int getId() {
            return id;
        }
        public void setId(int id) {
            this.id = id;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getPwd() {
            return pwd;
        }
        public void setPwd(String pwd) {
            this.pwd = pwd;
        }
        @Override
        public String toString() {
            return "User [id=" + id + ", name=" + name + ", pwd=" + pwd + "]";
        } 
    }

    user-mappers.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    <!-- 命名空间规则   本包下的类名+Mapper   -->
    <mapper namespace="cn.zys.entity.UserMapper">
        <select id="selectUser" resultType="User">
            select * from user where id = #{id}
        </select>
        
        <!-- 查询所有 -->
        <select id="selectAll" resultType="User" 返回值类型 >
            select * from user
        </select>
        <!-- 分页 -->
        <select id="selectLimit" parameterType="Map" resultType="User">
            select * from user limit #{startindex},#{pagenum}
        </select>
        
        <!-- 使用rowbangs分页 -->
            <select id="selectLimits" parameterType="Map" resultType="User">
            select * from user
        </select>
        <insert id="insertUser" parameterType="User" 此处的属性为参数类型 useGeneratedKeys="true" 此处属性为是否使用自增字段 >   增删改默认返回int类型
            insert into user(name,pwd) values(#{name},#{pwd})
        </insert>
        
        <update id="updateUser" parameterType="User" >
            update user set name=#{name},pwd=#{pwd} where id=#{id}
        </update>
        
        <delete id="deleteUser" >
            delete from user where id=#{id}
        </delete>
    </mapper>

    MyBatisUtils.java

    package cn.zys.until;
    
    import java.io.IOException;
    import java.io.InputStream;
    
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    
    public class MyBatisUtils {
        public static SqlSessionFactory getSqlSessionFactory() throws IOException{
            String resource = "mybatis.config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
            return sqlSessionFactory;
        }
        
        public static SqlSession getSqlSession() throws IOException{
            SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
            return sqlSessionFactory.openSession();
        }
    }

    db.properties

    driver=com.mysql.jdbc.Driver
    url=jdbc:mysql://localhost:3306/mybatis1
    username=root
    password=root

    mybatis.config.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
    PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
        <!-- 外部引入数据库配置 -->
        <properties resource="db.properties"></properties>
        
        <!-- 为某个类型指定别名   方便在某个引用时使用    比如传值 在此处使用在 user-mappers.xml的resultType -->
        <typeAliases>
            <!-- <typeAlias type="cn.zys.entity.User" alias="User" /> -->
            <!-- 为某个包下的所有类指定别名  默认别名是对应类名   -->
            <package name="cn.zys.entity"  />
        </typeAliases>
        
        <environments default="development">
            <environment id="development">
        <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
            </environment>
        </environments>
    <mappers>
        <mapper resource="cn/zys/entity/user-mappers.xml"/>
    </mappers>
    </configuration>

    test.java

    package cn.zys.test;
    
    import java.io.IOException;
    import java.util.List;
    
    
    import cn.zys.dao.UserDao;
    import cn.zys.entity.User;
    
    public class test {
        public static void main(String[] args) throws IOException {
            UserDao userdao = new UserDao();
         

          //新用户
          /* User user = new User();
          user.setName("002");
          user.setPwd("002");
          userdao.addUser(user);*/

          //分页测试
          List<User> list = userdao.selectLimts(0, 2);
          System.out.println(list);

        }
    }

    最后的测试中只测试了最后一个查询多条数据

  • 相关阅读:
    TI达芬奇平台测试
    V4L2 常用控制命令
    Linux 线程编程
    HMM
    PCA & ICA
    SVD分解(转载)
    Factor analysis
    Mixtures of Gaussians and the EM algorithm
    Arduino 单个按键控制led灯闪烁、常亮、关闭
    django计算当前日期最近一周日期范围
  • 原文地址:https://www.cnblogs.com/xiaozhang666/p/11631023.html
Copyright © 2020-2023  润新知