Mybatis1.1——使用mapper代理開發dao

海棠花開發表於2018-11-05

Javaweb框架學習文章索引點這裡
由於在dao介面實現類方法中呼叫運算元據庫的方法中存在大量冗餘程式碼,並且在呼叫方法的時候傳入了部分硬編碼引數等問題。引入mapper代理的方式可以解決這些問題,並提高效率。
下面是一個簡單的mapper代理開發的示例:
1,編寫javabean,以及對應的資料庫表。User(id, name, age,tel)
2,編寫UserMapper.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">

<!--這裡namespace填的是javabean的全限定類名-->
<mapper namespace="com.hhh.mapper.UserMapper">
		<select id="findUserById" parameterType="int" resultType="com.hhh.dao.User">
			select * from user where id = #{id}
		</select>
		
		<select id="findUserByName" parameterType="java.lang.String" resultType="com.hhh.dao.User">
			select * from user where name like '%${value}%'
		</select>
		
		<insert id="insertInfoToUser" parameterType="com.hhh.dao.User">
			insert into user(name, age, tel) values(#{name},#{age},#{tel})
			<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
				select last_insert_id();
			</selectKey>
		</insert>
		
		<delete id="deleteById" parameterType="java.lang.Integer">
			delete from user where id = #{id}
		</delete>
		
		<update id="updateUser" parameterType="com.hhh.dao.User">
			update user set name = #{name} where id = #{id}
		</update>
</mapper>

以上配置的sql語句主要是常見的增刪改查
3,在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>
	<environments default="development">
		<environment id="development">
			<!-- 使用jdbc事務管理 -->
			<transactionManager type="JDBC"/>
			<!-- 配置資料庫連線池 -->
			<dataSource type="POOLED">
				<property name="driver" value="com.mysql.jdbc.Driver"/>
				<property name="url" value="jdbc:mysql://localhost:3306/test"/>
				<property name="username" value="root"/>
				<property name="password" value="123456"/>
			</dataSource>					
		</environment>
	</environments>
	
	<!-- 載入對映檔案 -->
	<mappers>
		<mapper resource="mapper/UserMapper.xml"/>
	</mappers>
	
</configuration>

4,編寫UserMapper介面

package com.hhh.mapper;


import java.util.List;

import com.hhh.dao.User;

public interface UserMapper {
	/*
	 * 這裡的方法名需要和xml中配置的名稱相對應
	 * 輸入引數型別和返回值型別也是要和xml中配置的相同
	 * */
	public User findUserById(int id) throws Exception;
	
	/*
	 * 雖然在xml中配置的返回值型別為User,但是由於返回值為多項內容,所以這裡需要設定為List
	 * */
	public List<User> findUserByName(String name) throws Exception;
	
	public void deleteUserById(int id) throws Exception;
	
	public void updateUser(User user) throws Exception;
	
	public void insertInfoToUser(User user) throws Exception;
}

5,編寫測試:

@Test
	public void test2() throws Exception{
		//1,獲取sqlsession
		InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
		SqlSession sqlSession = sqlSessionFactory.openSession();
		
		//獲取由mybatis生成的mapper實現類
		UserMapper mapper = sqlSession.getMapper(UserMapper.class);
		
		//測試單項查詢
/*		User user= mapper.findUserById(1);
		System.out.println(user);*/
		
		//測試多項查詢
/*		List<User> list = mapper.findUserByName("張三");
		System.out.println(list);*/
		
		//測試插入
/*		User user = new User("張三3",23,"12344");
		mapper.insertInfoToUser(user);
		sqlSession.commit();*/
		
		//測試更新
/*		User user = new User("張三5",23,"12344");
		user.setId(23);//設定更新id為23的資料內容
		mapper.updateUser(user);
		sqlSession.commit();*/
		
		//測試刪除
		mapper.deleteUserById(23);
		sqlSession.commit();
	}

相關文章