源码:https://github.com/carryLess/mbtsstd-004
0.readme
基于前几篇:dao的实现类基本煤气到什么作用
仅仅是通过SQLSession的相应API定位到mapper映射文件中指定id的sql语句,
所以此刻,我们将实现类去掉,直接定位到映射文件的sql语句,
这种对Dao的实现方式称为动态代理
此方式下需要注意:
1.映射文件mapper标签中的namespace属性为Dao的全限定类名
2.Dao接口中的方法名称要与mapper映射文件中sql标签的id一致
3.dao对象的获取通过SqlSession.getMapper(dao.class);
4.sqlSession.commit();注意添加
5.动态代理下系统只会调用selectOne()或者selectList()方法,不支持返回Map的方法
1.主配置文件
<?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"> <!-- (以上)文件头在解压的文件夹中mybatis-3.4.4.pdf文件中搜索mybatis-3-config.dtd即可得到 --> <configuration> <!-- 指定属性配置文件 --> <properties resource="jdbc.properties" /> <!-- 配置类的别名,我建议使用package这种写法 这样写会将该包中所有类的简单类名配置为别名,简单方便 ,还有别的写法,自行google --> <typeAliases> <package name="model" /> </typeAliases> <!-- 配置MyBatis运行环境 --> <environments default="development"> <environment id="development"> <!-- 使用JDBC事务管理 --> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> </dataSource> </environment> </environments> <!-- 注册映射文件 --> <mappers> <mapper resource="dao/mapper.xml"/> <!-- 实际开发中可能有多个映射文件,而其中sql标签的id相同时候,执行过程就会报错 我们可以根据mapper映射文件中的namespace属性来区分,调用时候用如下方式 namespace.id --> <!-- <mapper resource="dao/mapper2.xml"/> --> </mappers> </configuration>
2.映射文件
<?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="dao.IStudentDao"> <insert id="insertStudentByList"> insert into SStudent(sname,sage,score) values <!-- 这里面的collection必须写成list --> <foreach collection="list" separator="," item="stu"> (#{stu.name},#{stu.age},#{stu.score}) </foreach> </insert> <!-- 使用别名 --> <select id="selectById1" resultType="SStudent"> select sid id,sname name,sage age,score from sstudent where sid = #{xxx} </select> <!-- 使用resultMap --> <select id="selectById2" resultMap="sStudentMapper"> select sid,sname,sage,score from sstudent where sid = #{xxx} </select> <!-- type:要映射的实体类 id:resultMap标签的id,用于select标签中resultMap属性 --> <resultMap id="sStudentMapper" type="SStudent"> <id column="sid" property="id" /> <result column="sname" property="name" /> <result column="sage" property="age" /> </resultMap> <!-- 通过map接收多个参数 --> <select id="selectByMap" resultMap="sStudentMapper"> select * from sstudent where sname like '%' #{nameCon} '%' and sage > #{ageCon} </select> <select id="selectByCons" resultMap="sStudentMapper"> select * from sstudent where sname like '%' #{name} '%' and sage > #{age} </select> </mapper>
3.dao接口
package dao; import model.SStudent; import org.apache.ibatis.annotations.Param; import java.util.List; import java.util.Map; /** * Created by carryLess on 2017/11/29. */ public interface IStudentDao { /** * 插入集合 * @param studentList */ void insertStudentByList(List<SStudent> studentList); /** * 根据id查询1 * @param id * @return */ SStudent selectById1(int id); /** * 根据id查询2 * @param id * @return */ SStudent selectById2(int id); /** * 根据map查询 map中key为:nameCon和ageCon * @param conMap * @return */ List<SStudent> selectByMap(Map<String,Object> conMap); /** * 多参数查询,其中@Param中的value是字段名称 * @param name * @param age * @return */ List<SStudent> selectByCons(@Param("name") String name, @Param("age") int age); }
4.test
package test; import dao.IStudentDao; import model.SStudent; import org.apache.ibatis.session.SqlSession; import org.junit.After; import org.junit.Before; import org.junit.Test; import utils.MyBatisUtils; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by carryLess on 2017/11/29. */ public class MyTest { private SqlSession sqlSession; private IStudentDao dao; @Before public void initDao(){ sqlSession = MyBatisUtils.getSqlSession(); dao = sqlSession.getMapper(IStudentDao.class); } @After public void over(){ if(sqlSession != null){ sqlSession.close(); } } @Test public void testInsertList(){ List<SStudent> sStudentList = new ArrayList<SStudent>(); for(int i = 20;i<22;i++){ SStudent sStudent = new SStudent(); sStudent.setSname("ls-"+i); sStudent.setSage(25+i); sStudent.setScore(90); sStudentList.add(sStudent); } dao.insertStudentByList(sStudentList); sqlSession.commit(); } @Test public void testSelectById(){ SStudent sStudent = dao.selectById2(18); System.out.println(sStudent); } @Test public void testSelectByMap(){ Map<String,Object> conMap = new HashMap<String,Object>(); conMap.put("nameCon","s"); conMap.put("ageCon",44); List<SStudent> sStudentList = dao.selectByMap(conMap); System.out.println(sStudentList); } @Test public void testSelectByCons(){ List<SStudent> sStudentList = dao.selectByCons("s", 44); System.out.println(sStudentList); } }