
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">
<!--
需求:根據(jù)id獲取用戶信息
id:表示表示映射文件中的sql,將sql語句封裝到mappedStatement對象中,將id稱為Statement的 id
parameterType:指定出入?yún)?shù)類型
resultType:指定輸出參數(shù)類型
#{}表示一個占位符
#{id}接收輸入?yún)?shù),參數(shù)名為id,如果輸入的是簡單類型,#{}中的參數(shù)名可以任意
-->
<select id="findUserById" parameterType="int" resultType="cn.ztc.mybatis.po.User">
select * from user where id = #{id}
</select>
<!--
需求:自定義條件查詢用戶列表 ,可能返回多條數(shù)據(jù)
resultType:指定的就是單條記錄所使用的java對象類型
${}表示拼接sql串,將接收到的參數(shù)不加任何修飾拼接在sql串中
使用${}可能會引起sqk注入
${value}接收輸入?yún)?shù),如果輸入的是簡單類型,#{}中的參數(shù)名只能是value
-->
<select id="findUserByName" parameterType="java.lang.String" resultType="cn.ztc.mybatis.po.User">
select * from user where username like '%${value}%'
</select>
<!--
添加用戶
parameterType:輸入?yún)?shù)類型是pojo
#{}中指定pojo的屬性名
-->
<insert id="insertUser" parameterType="cn.ztc.mybatis.po.User">
<!--
將插入數(shù)據(jù)庫的主鍵返回到對象中
select LAST_INSERT_ID():得到insert記錄的主鍵值,只適用于自增主鍵
keyProperty:將查詢到的主鍵值設(shè)置到parameterType指定對象的某個屬性
order:相對于insert語句來說,select LAST_INSERT_ID()的執(zhí)行順序
-->
<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生成主鍵
執(zhí)行過程:
首先用過uuid()得到主鍵,將主鍵設(shè)置到user對象的id屬性中
其次在執(zhí)行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}:對應(yīng)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 {
//根據(jù)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實現(xiàn)類中注入SqlSessionFactory
//這里通過構(gòu)造函數(shù)注入
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;
//此方法在執(zhí)行testFindUserById方法之前執(zhí)行
@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 {
//創(chuàng)建UserDao對象
UserDao UserDao = new UserDaoImpl(sqlSessionFactory);
//調(diào)用UserDao方法
User user = UserDao.findUserById(28);
System.out.println(user);
}
}
總結(jié)原始Dao開發(fā)問題:
1.dao實現(xiàn)類方法中存在大量的模板方法,設(shè)想能否將這些代碼提取出來
2.調(diào)用sqlsession方法時,將statement的id硬編碼
3.調(diào)用sqlsession方法時,即使變量傳入錯誤類型,在編譯階段也不報錯