原始Dao开发及Mapper动态代理开发

一、原始Dao开发

1.映射文件(两种开发模式同一个映射文件,只是第二种方式的命名空间的名称必须和接口类路径一样):

<?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">
<!-- namespace命名空间,用于隔离sql -->
<mapper namespace="com.itheima.mybatis.mapper.UserMapper">
<!-- 通过id查询一个用户 -->
<select id="findUserById" parameterType="Integer" resultType="com.itheima.mybatis.pojo.User">
	select * from user where id=#{id}
</select>
<!-- 根据用户名称模糊查询用户列表 
	#{}占位符
	${}字符串拼接
-->
<select id="findUserByUsername" parameterType="String" resultType="com.itheima.mybatis.pojo.User">
	<!-- select * from user where username like '%${value}%'-->
	select * from user where username like "%"#{v}"%"
	<!--两条sql语句是一样的,第一句是select * from user where username like '%五%'
	第二句是select * from user where uername like "%"'五'"%" -->
</select>
<!-- 添加用户 -->
<insert id="insertUser" parameterType="com.itheima.mybatis.pojo.User">
	insert into user(username,birthday,address,sex)
	values (#{username},#{birthday},#{address},#{sex})
</insert>
<!-- 更新用户 -->
<update id="updateUserById" parameterType="com.itheima.mybatis.pojo.User">
	update user
	set username=#{username},sex=#{sex},birthday=#{birthday},address=#{address}
	where id=#{id}
</update>
<!-- 删除 -->
<delete id="deleteUserById" parameterType="Integer">
	delete from user where id=#{id}
</delete>
</mapper>

2.Dao接口以及Dao实现类

public interface UserDao {
	//通过用户id查询一个用户
	public User selectUserById(Integer id);
}
public class UserDaoImpl implements UserDao {
	//构造器注入
	private SqlSessionFactory sqlSessionFactory;
	public UserDaoImpl(SqlSessionFactory sqlSessionFactory) {
		super();
		this.sqlSessionFactory = sqlSessionFactory;
	}
	//通过用户id查询一个用户
	public User selectUserById(Integer id){
		SqlSession sqlSession=sqlSessionFactory.openSession();
		return sqlSession.selectOne("user.findUserById",id);
	}
}

3.测试类

public class MybatisDaoTest {
	private SqlSessionFactory sqlSessionFactory;
	@Before
	public void before() throws IOException{
		//加载核心配置文件
		String resource="sqlMapConfig.xml";
		InputStream in=Resources.getResourceAsStream(resource);
		//创建sqlsessionFactory
		sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
	}
	@Test
	public void testDao(){
		UserDao userDao=new UserDaoImpl(sqlSessionFactory);
		User user=userDao.selectUserById(10);
		System.out.println(user);
	}
}

4.原始Dao开发存在以下问题

u Dao方法体存在重复代码:通过SqlSessionFactory创建SqlSession,调用SqlSession的数据库操作方法

调用sqlSession的数据库操作方法需要指定statementid,这里存在硬编码,不得于开发维护。

二、Mapper动态代理方式

1.Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。

2.Mapper接口开发需要遵循以下规范:

1、 Mapper.xml文件中的namespacemapper接口的类路径相同。

2、 Mapper接口方法名和Mapper.xml中定义的每个statementid相同

3、 Mapper接口方法的输入参数类型和mapper.xml中定义的每个sql parameterType的类型相同

4、 Mapper接口方法的输出参数类型和mapper.xml中定义的每个sqlresultType的类型相同

3.接口

public interface UserMapper {
//遵循四个原则
	//接口方法名==user.xml中id名
	//接口中返回值类型与Mapper.xml中的返回值类型一致
	//方法的入参类型与mapper.xml中入参类型一致
	//命名空间绑定此接口:namespacemapper接口的类路径相同
public User findUserById(Integer id);
}

4.测试

public class MybatisMapperTest {
@Test
public void testMapper() throws IOException{
	//加载核心配置文件
	String resource="sqlMapConfig.xml";
	InputStream in=Resources.getResourceAsStream(resource);
	//创建sqlsessionfactory
	SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(in);
	//创建sqlsession
	SqlSession sqlSession=sqlSessionFactory.openSession();
	//sqlsession帮我生成一个实现类给接口
	UserMapper userMapper=sqlSession.getMapper(UserMapper.class);
	User user=userMapper.findUserById(10);
	System.out.println(user);
}
}


猜你喜欢

转载自blog.csdn.net/qq_36594703/article/details/80377007