一 mapper接口动态代理
传统的Dao开发方式
Dao需求
1.根据用户id查询用户信息
2.根据用户名查找用户列表
3.添加用户
原始Dao开发方法
1.新建映射文件编写SQL
2.新建接口
3.新建实现类
4.测试
接口类
package com.yuyi.dao;
import java.util.List;
import com.yuyi.pojo.Order;
import com.yuyi.pojo.QueryVo;
import com.yuyi.pojo.User;
public interface UserMapper {
User getUserById(int id);
List<User> getUserByName(String name);
void insertUser(User user);
User getUserByQueryVo(QueryVo queryVo);
List<Order> getOrderList();
}
实现类
package com.yuyi.dao.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.yuyi.dao.UserMapper;
import com.yuyi.pojo.Order;
import com.yuyi.pojo.QueryVo;
import com.yuyi.pojo.User;
import com.yuyi.util.SqlSessionFactoryUtils;
public class UserDaoimpl implements UserMapper{
@Override
public User getUserById(int id) {
SqlSession session=SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
User user=session.selectOne("getUserById", id);
session.close();
return user;
}
@Override
public List<User> getUserByName(String name) {
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
List<User> list = sqlSession.selectList("getUserByName",name);
sqlSession.close();
return list;
}
@Override
public void insertUser(User user) {
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
sqlSession.insert("insertUser",user);
sqlSession.commit();
sqlSession.close();
}
@Override
public User getUserByQueryVo(QueryVo queryVo) {
// TODO Auto-generated method stub
return null;
}
@Override
public List<Order> getOrderList() {
// TODO Auto-generated method stub
return null;
}
}
三 测试类
package com.yuyi.test;
import java.util.List;
import org.junit.Test;
import com.yuyi.dao.UserMapper;
import com.yuyi.dao.impl.UserDaoimpl;
import com.yuyi.pojo.User;
public class UserDaoimplTest {
@Test
public void testGetUserById() {
UserMapper dao=new UserDaoimpl();
User user=dao.getUserById(10);
System.out.println(user);
}
@Test
public void testGetUserByName() {
UserMapper dao = new UserDaoimpl();
List<User> user = dao.getUserByName("張");
for (User user2 : user) {
System.out.println(user2);
}
}
@Test
public void testInsertUser() {
UserMapper dao = new UserDaoimpl();
User user2=new User();
user2.setUsername("xxoo");
user2.setAddress("tianjian");
dao.insertUser(user2);
}
}
从中我们可以发现重复的代码太多,所以使用mapper接口动态代理的方式
使用mapper接口动态代理的方式
动态代理dao开发规则
1.namespace必须是接口的全类名
2.接口的方法名必须和映射文件的sql id一致
3.接口的输入参数必须和映射文件的parameterType类型一致
4.接口的返回类型必须与映射文件的resultType类型一致
动态代理dao开发步骤
1.创建xxxMapper.xml映射文件 编写sql
2.创建接口
3.加载xxxMapper.xml
4.测试动态代理Dao
UserMpper.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="com.yuyi.dao.UserMapper">
<select id="getUserById" parameterType="int" resultType="user">
select
`id`,
`username`,
`birthday`,
`sex`,
`address`
from `user`
where id =#{id}
</select>
<select id="getUserByName" parameterType="string" resultType="user">
select
`id`,
`username`,
`birthday`,
`sex`,
`address`
from `user`
where username like '%${value}%'
</select>
<insert id="insertUser" parameterType="user" useGeneratedKeys="true" keyProperty="id">
<!--
keyProperty 主键返回
resultType user中的主键的属性
order 指定 selectKey 何时执行 AFTER 是之前执行
-->
<!-- <selectKey keyProperty="id" resultType="int" order="AFTER">
select last_insert_id()
</selectKey> -->
insert into `user`(
`username`,
`birthday`,
`sex`,
`address`,
`uuuid`)
values (
#{username},
#{birthday},
#{sex},
#{address},
#{uuuid}
)
</insert>
<select id="getUserByQueryVo" parameterType="queryVo" resultType="user">
select
`id`,
`username`,
`birthday`,
`sex`,
`address`
from `user`
where id =#{user.id}
</select>
<select id="getOrderList" resultMap="order_user_list">
select
`id`,
`user_id`,
`number`,
`createtime`,
`note`
from `order`
</select>
<!--type:类型 -->
<resultMap type="order" id="order_user_list">
<!-- id主键的映射
property 是pojo中的主键属性
column 是返回结果中主键的列
-->
<id property="userid" column="user_id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
</resultMap>
</mapper>
测试类
package com.yuyi.test;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import com.yuyi.dao.UserMapper;
import com.yuyi.pojo.Order;
import com.yuyi.pojo.QueryVo;
import com.yuyi.pojo.User;
import com.yuyi.util.SqlSessionFactoryUtils;
public class UserMapperTest {
@Test
public void testGetUserById() {
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user=userMapper.getUserById(10);
System.out.println(user);
sqlSession.close();
}
@Test
public void testGetUserByName() {
}
@Test
public void testInsertUser() {
}
@Test
public void testGetUserByQueryVo() {
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
QueryVo queryVo=new QueryVo();
User user=new User();
user.setId(1);
queryVo.setUser(user);
User user2=userMapper.getUserByQueryVo(queryVo);
System.out.println(user2);
sqlSession.close();
}
@Test
public void testGetOrderList(){
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<Order> orderList=userMapper.getOrderList();
for (Order order : orderList) {
System.out.println(order);
}
sqlSession.close();
}
}
二 SqlMapConfig.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>
<!-- 先加載內部配置再去加載外部的標籤 名稱不一致 會被替換 -->
<properties resource="jdbc.properties">
<property name="jdbc.username" value="root" />
<property name="jdbc.password" value="628081" />
</properties>
<!--单个别名配置 -->
<typeAliases>
<!--
type: 全路径名
alias:自定义的别名 (别名不区分大小写)
-->
<!-- <typeAlias type="com.yuyi.pojo.User" alias="user"/>-->
<!-- 包扫描的配置 name:这个是你要扫描的包 别名:就是类的名称 不区分大小写 -->
<package name="com.yuyi.pojo" />
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<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="mybatis/user.xml"/>
<mapper resource="com/yuyi/mapper/UserMapper.xml"/>
-->
<!-- <mapper resource="com/yuyi/dao/UserMapper.xml"/>-->
<package name="com.yuyi.dao"/>
</mappers>
</configuration>
properties属性
<properties resource="jdbc.properties">
<property name="jdbc.username" value="root" />
<property name="jdbc.password" value="628081" />
</properties>
jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=xxx
typeAliases
单个别名配置
<typeAlias type="com.yuyi.pojo.User" alias="user"/>
包扫描别名配置
<!-- 包扫描的配置 name:这个是你要扫描的包 别名:就是类的名称 不区分大小写 -->
<package name="com.yuyi.pojo" />
</typeAliases>
Mapper(映射器)配置的几种方法
三 传递包装的pojo
实体类
package com.yuyi.pojo;
public class QueryVo {
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
}
配置xxxMapper.xml文件
<select id="getUserByQueryVo" parameterType="queryVo" resultType="user">
select
`id`,
`username`,
`birthday`,
`sex`,
`address`
from `user`
where id =#{user.id}
</select>
接口中添加方法
User getUserByQueryVo(QueryVo queryVo);
测试
@Test
public void testGetUserByQueryVo() {
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
QueryVo queryVo=new QueryVo();
User user=new User();
user.setId(1);
queryVo.setUser(user);
User user2=userMapper.getUserByQueryVo(queryVo);
System.out.println(user2);
sqlSession.close();
}
四 ResultMap
实体类
package com.yuyi.pojo;
import java.util.Date;
public class Order {
private Integer id;
private Integer userid;
private String number;
private Date createtime;
private String note;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUserid() {
return userid;
}
public void setUserid(Integer userid) {
this.userid = userid;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
public Date getCreatetime() {
return createtime;
}
public void setCreatetime(Date createtime) {
this.createtime = createtime;
}
public String getNote() {
return note;
}
public void setNote(String note) {
this.note = note;
}
@Override
public String toString() {
return "Order [id=" + id + ", userid=" + userid + ", number=" + number + ", createtime=" + createtime
+ ", note=" + note + "]";
}
}
配置xxxMapper.xml
<
select id="getOrderList" resultMap="order_user_list">
select
`id`,
`user_id`,
`number`,
`createtime`,
`note`
from `order`
</select>
<!--type:类型 -->
<resultMap type="order" id="order_user_list">
<!-- id主键的映射
property 是pojo中的主键属性
column 是返回结果中主键的列
-->
<id property="userid" column="user_id"/>
<result property="number" column="number"/>
<result property="createtime" column="createtime"/>
<result property="note" column="note"/>
</resultMap>
编写接口
List<Order> getOrderList();
测试
@Test
public void testGetOrderList(){
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
List<Order> orderList=userMapper.getOrderList();
for (Order order : orderList) {
System.out.println(order);
}
sqlSession.close();
}