美文网首页
原始dao开发方法(程序员需要写dao接口和dao实现类)

原始dao开发方法(程序员需要写dao接口和dao实现类)

作者: Stringer | 来源:发表于2016-11-20 23:05 被阅读1762次
2016-11-20_173419.png

User.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 namespace="test">
    <!-- 
          需求:根据id获取用户信息
         id:表示表示映射文件中的sql,将sql语句封装到mappedStatement对象中,将id称为Statement的 id
         parameterType:指定出入参数类型
         resultType:指定输出参数类型
         #{}表示一个占位符
         #{id}接收输入参数,参数名为id,如果输入的是简单类型,#{}中的参数名可以任意
    -->
    <select id="findUserById" parameterType="int" resultType="cn.ztc.mybatis.po.User">
        select * from user where id = #{id}
    </select>
    
    <!-- 
        需求:自定义条件查询用户列表 ,可能返回多条数据
        resultType:指定的就是单条记录所使用的java对象类型
        ${}表示拼接sql串,将接收到的参数不加任何修饰拼接在sql串中
        使用${}可能会引起sqk注入
        ${value}接收输入参数,如果输入的是简单类型,#{}中的参数名只能是value
    -->
    <select id="findUserByName" parameterType="java.lang.String" resultType="cn.ztc.mybatis.po.User">
        select * from user where username like '%${value}%'
    </select>
    
    <!-- 
        添加用户
        parameterType:输入参数类型是pojo
        #{}中指定pojo的属性名
     -->
    <insert id="insertUser" parameterType="cn.ztc.mybatis.po.User">
        <!-- 
            将插入数据库的主键返回到对象中
            select LAST_INSERT_ID():得到insert记录的主键值,只适用于自增主键
            keyProperty:将查询到的主键值设置到parameterType指定对象的某个属性
            order:相对于insert语句来说,select LAST_INSERT_ID()的执行顺序
         -->
        <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
            select LAST_INSERT_ID()
        </selectKey>
        insert into user (username,birthday,sex,address) values (#{username},#{birthday},#{sex},#{address})
        <!-- 
            使用mysql的uuid生成主键
            执行过程:
            首先用过uuid()得到主键,将主键设置到user对象的id属性中
            其次在执行insert,从user对象中取出id属性值
         -->
         <!-- <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">
            select uuid()
        </selectKey>
        insert into user (id,username,birthday,sex,address) values (#{id},#{username},#{birthday},#{sex},#{address}) -->
    </insert>
    
    <!-- 删除用户 -->
    <delete id="deleteUser" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    </delete>
    
    <!-- 
        更新用户 
        #{id}:对应pojo的属性值
    -->
    <update id="updateUser" parameterType="cn.ztc.mybatis.po.User">
        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id = #{id}
    </update>
</mapper>

UserDao.java

package cn.ztc.mybatis.dao;

import cn.ztc.mybatis.po.User;

public interface UserDao {
    
    //根据id查用户信息
    public User findUserById(int id) throws Exception;
    
    //添加用户信息
    public void insertUser(User user) throws Exception;
    
    //删除用户信息
    public void deleteUser(int id) throws Exception;
    
}

UserDaoImpl.java

package cn.ztc.mybatis.dao;

import java.util.Date;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import cn.ztc.mybatis.po.User;

public class UserDaoImpl implements UserDao{
    
    //需要向dao实现类中注入SqlSessionFactory
    //这里通过构造函数注入
    private SqlSessionFactory sqlSessionFactory;
    public UserDaoImpl(SqlSessionFactory sqlSessionFactory){
        this.sqlSessionFactory = sqlSessionFactory;
    }

    @Override
    public User findUserById(int id) throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        User user = sqlSession.selectOne("test.findUserById", id);
        sqlSession.close();
        return user;
    }

    @Override
    public void insertUser(User user) throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.insert("test.insertUser", user);
        sqlSession.commit();
        sqlSession.close();
    }

    @Override
    public void deleteUser(int id) throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        sqlSession.delete("test.deleteUser", id);
        sqlSession.commit();
        sqlSession.close();
    }

}

UserDaoImplTest.java

package cn.ztc.mybatis.dao;

import static org.junit.Assert.*;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import cn.ztc.mybatis.po.User;

public class UserDaoImplTest {
    
    private SqlSessionFactory sqlSessionFactory;
    //此方法在执行testFindUserById方法之前执行
    @Before
    public void setUp() throws Exception{
        String resource = "SqlMapConfig.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
    }
    

    @Test
    public void testFindUserById() throws Exception {
        //创建UserDao对象
        UserDao UserDao = new UserDaoImpl(sqlSessionFactory);
        
        //调用UserDao方法
        User user = UserDao.findUserById(28);
        
        System.out.println(user);
    }

}

总结原始Dao开发问题:
1.dao实现类方法中存在大量的模板方法,设想能否将这些代码提取出来
2.调用sqlsession方法时,将statement的id硬编码
3.调用sqlsession方法时,即使变量传入错误类型,在编译阶段也不报错

相关文章

网友评论

      本文标题:原始dao开发方法(程序员需要写dao接口和dao实现类)

      本文链接:https://www.haomeiwen.com/subject/iskypttx.html