Mybatis使用原始dao开发

存在的问题:

1、Dao方法体存在重复代码:通过SqlSessionFactory创建SqlSession,调用SqlSession的数据库操作方法
2、调用sqlSession的数据库操作方法需要指定statement的id,这里存在硬编码,不得于开发维护。

sql映射文件

<mapper namespace="xx">
     <!-- 根据id查询 -->
    <select id="queryUserById" parameterType="int" resultType="com.mybatis.entity.User" >
        select *  from  t_user where id = #{id}
    </select>
    <!-- 查询所有用户 -->
    <select id="findAll" resultType="com.mybatis.entity.User">
        select * from t_user
    </select>
    <!-- 根据姓名模糊查询 -->
    <select id="findListByName" parameterType="String" resultType="com.mybatis.entity.User">
        select * from t_user where username like '%${value}%'
    </select>
    <!-- 添加 -->
    <insert id="insertUser" parameterType="com.mybatis.entity.User" >
        insert into t_user values(sep_user.nextval,#{username},#{password},#{sex},#{brithday},#{address})
    </insert>
    <!-- 删除 -->
    <delete id="deleteUser" parameterType="int">
        delete from t_user where id = #{id}
    </delete>
    <!-- 修改 -->
    <update id="updateUser" parameterType="com.mybatis.entity.User">
        update t_user set username = #{username},password = #{password},sex = #{sex},brithday = #{brithday},
        address = #{address} where id = #{id}
    </update>
</mapper>

UserDao接口:

//根据id查询用户
User selectUser(int id);
    
//查询所有用户
List<User> findAll();
    
//根据姓名模糊查询
List<User> selectUserByName(String username);
    
//添加用户(其中返回类型可以改为void)
int insertUser(User user);
    
//修改用户
int updateUser(User user);
    
//根据id删除用户
int deleteUser(int id);

定义一个MybatisUtil工具类,用于创建SqlSession:

public class MybatisUtil {
    //将sqlsession工厂定义在外面,方便获取sqlsession方法,
    private static SqlSessionFactory sqlSessionFactory ;
    
    //静态方法,调用直接创建
    static{
        InputStream in = null;
        try {
            in = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            sqlSessionFactory = builder.build(in);
        
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    //打开sqlsession
    public static SqlSession openSession(){
        return sqlSessionFactory.openSession();
    }

UserDaoImpl实现类:

   //根据id查询
    @Override
    public User selectUser(int id) {
        SqlSession sqlSession = MybatisUtil.openSession();
        User user = sqlSession.selectOne("xx.queryUserById", id);
        sqlSession.close();
        return user;
    }
    //查询所有用户
    @Override
    public List<User> findAll() {
        SqlSession sqlSession = MybatisUtil.openSession();
        List<User> user = sqlSession.selectList("xx.findAll");
        sqlSession.close();
        return user;
    }
    //根据姓名模糊查询
    @Override
    public List<User> selectUserByName(String username) {
        SqlSession sqlSession = MybatisUtil.openSession();
        List<User> user = sqlSession.selectList("xx.findListByName", username);
        sqlSession.close();
        return user;
    }
    //添加用户
    @Override
    public int insertUser(User user) {
        SqlSession sqlSession = MybatisUtil.openSession();
        int i = sqlSession.insert("xx.insertUser",user);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }
    //修改用户
    @Override
    public int updateUser(User user) {
        SqlSession sqlSession = MybatisUtil.openSession();
        int i = sqlSession.update("xx.updateUser",user);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }
    //删除用户
    @Override
    public int deleteUser(int id) {
        SqlSession sqlSession = MybatisUtil.openSession();
        int i = sqlSession.delete("xx.deleteUser",id);
        sqlSession.commit();
        sqlSession.close();
        return i;
    }

实现类中大量重复代码,

测试类:

//调用实现类中的方法:
UserDao userDao = new UserDaoImpl();
//根据id查找 //User user = userDao.selectUser(25); //System.out.println(user); //查询所有的商品 /*List<User> list = userDao.findAll(); for (User user : list) { System.out.println(user); }*/ //按条件模糊查询 /*List<User> list = userDao.selectUserByName("李四"); for (User user : list) { System.out.println(user); }*/ //添加 /*User user = new User(); user.setUsername("张三"); user.setPassword("666"); user.setSex("男"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); user.setBrithday(sdf.parse("1998-9-1")); user.setAddress("泰和"); int i= userDao.insertUser(user); System.out.println("执行了"+i);*/ //修改 /*User user = new User(); user.setUsername("啊啊"); user.setPassword("666"); user.setSex("男"); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd"); user.setBrithday(sdf.parse("1998-9-1")); user.setAddress("深圳"); user.setId(25); int i= userDao.updateUser(user); System.out.println("执行了"+i);*/ //删除 int i= userDao.deleteUser(25); System.out.println("执行了"+i);

 

posted @ 2020-05-30 10:42  64Byte  阅读(132)  评论(0编辑  收藏  举报