您的位置:首页 > 移动开发

MyBatis Mapper动态代理详解

2019-03-25 16:27 791 查看

1、mapper动态代理的作用

mapper动态代理无需程序员实现Dao接口,接口由MyBatis结合映射文件自动生成的动态代理实现的。

2、修改映射文件的namespace属性

使用动态代理需要映射文件mapper标签中的namespace属性修改为Dao接口的全类名。

3、Dao接口方法命名

MyBatis框架要求,接口中的方法名必须与映射文件中的sql标签的id相同

4、Dao对象的获取

使用时只需要调用SqlSession的getMapper()方法,即可获取指定接口的类实现对象,该方法的参数为指定Dao接口的class值。

5、话不多说,下面上实例.实现对student数据库的增删改查。

相关jar包

结构目录

1、首先我们新建一个student数据库

2、新建Student类,其中包含数据库中属性,这里太简单就不把代码放上来了。
3、新建Dao接口StudentDao

public interface StudentDao {
//插入学生
public void insertStudent(Student student);
//删除学生
public void deleteStudentById(int id);
//修改学生信息
public void updateStudent(Student student);
//查询所有学生信息
public List<Student> selectAllStudent();
//使用if标签动态查询学生信息
public List<Student> selectByIf(Student student);
}

4、新建映射文件UserMapper.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org/DTD Mapper 3.0"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.my.dao.StudentDao">

<!-- 添加学生 -->
<insert id="insertStudent" parameterType="com.my.student.Student"
useGeneratedKeys="true" keyProperty="id">
insert into student(name,age,score) values(#{name},#{age},#{score})
<!-- </selectKey>用于获取新插入记录的主键值 -->
<selectKey resultType="int" keyProperty="id" order="AFTER">
<!-- select @@identity -->
select last_insert_id();
</selectKey>
</insert>

<!-- 删除学生 -->
<delete id="deleteStudentById">
delete from student where id=#{xxx}
</delete>

<!-- 修改学生信息 -->
<update id="updateStudent">
update student set name=#{name},age=#{age},score=#{score} where id=#{id}
</update>

<!-- 查询所有学生 ,返回list -->
<select id="selectAllStudent" resultType="com.my.student.Student">
select * from student
</select>

<!-- if标签动态查询 -->
<select id="selectByIf" resultType="com.my.student.Student">
select * from student where 1=1
<if test="name!=null and name !=''">
and name like '%' #{name} '%'
</if>
<if test="age > 0">
and age > #{age}
</if>

</select>

<!-- 根据id查找学生 -->
<select id="selectById" resultType="com.my.student.Student">
select * from student wher
20000
e id= #{lll}
</select>

<!-- 根据姓名模糊搜索 -->
<select id="selectByName" resultType="com.my.student.Student">
<!-- select * from student where name like concat('%',#{kkk},'%') -->
select * from student where name like '%' #{ppp} '%'

</select>

<!-- resultmap别名查询,当数据库中属性名与bean属性名称不同时使用 -->
<resultMap type="com.my.student.Student" id="resultMapper">
<id column="id" property="id" />
<result column="name" property="name"/>
</resultMap>
<select id="selectResultMapper" resultMap="resultMapper">
select id, name, age,score from student where id=#{o}
</select>

</mapper>

5、工具类MyBatisUtil

public class MyBatisUtil {
private static SqlSessionFactory factory;

public static SqlSession getSqlSession() {
try {
if(factory==null) {

InputStream inputStream =Resources.getResourceAsStream("mybatis.cfg.xml");
factory=new SqlSessionFactoryBuilder().build(inputStream);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return factory.openSession();
}

}

6、xml文件mybatis.cfg.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>
<settings>
<setting name="logImpl" value="LOG4J" />
</settings>

<!-- 配置mybatis运行环境 -->
<environments default="mysql">
<environment id="mysql">
<!-- type="JDBC" 代表使用JDBC的提交和回滚来管理事务 -->
<transactionManager type="JDBC" />

<!-- POOLED 表示支持JDBC数据源连接池 -->
<!-- UNPOOLED 表示不支持数据源连接池 -->
<!-- JNDI 表示支持外部数据源连接池 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
此处为数据库链接
<property name="url" value="jdbc:mysql://localhost:3306/db_user" />
<property name="username" value="root" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>

<mappers>
<!-- 告知映射文件方式1,一个一个的配置-->
<mapper resource="com/my/mapper/UserMapper.xml"/>
</mappers>
</configuration>

7、测试类,没有导入junit包,就姑且使用main方法导入

public class DaiLiTest {
public static SqlSession session;
private static StudentDao dao;

public static void main(String[] args) {
session=MyBatisUtil.getSqlSession();
dao=session.getMapper(StudentDao.class);
Student stu=new Student();
stu.setId(19);
stu.setAnme("jack");
stu.setAge(13);
stu.setScore(70);

//插入测试
//		dao.insertStudent(stu);
//		session.commit();
//删除测试
//		dao.deleteStudentById(20);
//		session.commit();
//修改测试
//		dao.updateStudent(stu);
//		session.commit();
//查询测试
//		List<Student> list=dao.selectAllStudent();
//		for(Student stu1:list) {
//			System.out.println(stu1.getAnme());
//		}

//if标签动态查询
List<Student> list=dao.selectByIf(stu);
for(Student stu1:list) {
System.out.println(stu1.getAnme());
}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: