1. 程式人生 > >mybatis傳統方式開發DAO

mybatis傳統方式開發DAO

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>
	<!-- 和spring整合後 environments配置將廢除 -->
	<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/igeekmybatis?characterEncoding=utf-8" />
				<property name="username" value="root" />
				<property name="password" value="root" />
			</dataSource>
		</environment>
	</environments>
	
	<!-- 註冊對映檔案 -->
	<!-- 將對映檔案配置到mybatis的配置檔案中 -->
	<mappers>
		<mapper resource="sqlmap/User.xml"/>
	</mappers>
</configuration>

User.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:名稱空間,用於隔離sql,還有一個很重要的作用,後面會講 -->
<mapper namespace="test">
	<!-- 新增使用者 -->
	<insert id="save" parameterType="com.igeek.crm.pojo.User">
		insert into user(userName,birthday,sex,address)
		values(#{username},#{birthday},#{sex},#{address})
	</insert>
	
	<!-- 根據ID查詢使用者 -->
	<select id="queryById" parameterType="int" resultType="com.igeek.crm.pojo.User">
		select * from user where id = #{id}
	</select>
	
	<!-- 根據名稱模糊查詢 -->
	<select id="queryByName" parameterType="string" resultType="com.igeek.crm.pojo.User">
		select * from user where username like '%${value}%'
	</select>
	<!-- 查詢全部 -->
	<select id="queryAll" resultType="com.igeek.crm.pojo.User">
		select * from user
	</select>
	<!--  修改使用者資訊 -->
	<update id="update" parameterType="com.igeek.crm.pojo.User">
		update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id}
	</update>
	
	<!-- 根據ID刪除使用者 -->
	<delete id="delete" parameterType="int">
		delete from user where id = #{id}
	</delete>
</mapper>

UserDAOImpl

package com.igeek.crm.dao.impl;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import com.igeek.crm.dao.UserDAO;
import com.igeek.crm.pojo.User;

/**
 * @author www.igeehome.com
 * 
 *         TODO
 *
 *         2018年10月15日下午6:44:34
 */
public class UserDAOImpl implements UserDAO {

	private SqlSessionFactory factory;

	/**
	 * 構造方法中傳入工廠物件
	 * 
	 * @param factory
	 */
	public UserDAOImpl(SqlSessionFactory factory) {
		super();
		this.factory = factory;
	}

	@Override
	public int save(User user) {
		int result = -1;
		// 開啟session(建立一個sqlSession)
		SqlSession session = factory.openSession();
		// 通過session執行sql語句
		result = session.update("test.save", user);
		// 提交事務
		session.commit();
		// 關閉session
		session.close();
		return result;
	}

	@Override
	public User queryById(int id) {
		User u = null;
		// 開啟session(建立一個sqlSession)
		SqlSession session = factory.openSession();
		// 通過session執行sql語句
		u = session.selectOne("test.queryById", id);
		// 提交事務
		// session.commit();
		// 關閉session
		session.close();
		return u;
	}

	@Override
	public List<User> queryByName(String name) {
		List<User> users = null;
		// 開啟session(建立一個sqlSession)
		SqlSession session = factory.openSession();
		// 通過session執行sql語句
		users = session.selectList("test.queryByName", name);
		// 提交事務
		// session.commit();
		// 關閉session
		session.close();
		return users;
	}

	@Override
	public List<User> queryAll() {
		List<User> users = null;
		// 開啟session(建立一個sqlSession)
		SqlSession session = factory.openSession();
		// 通過session執行sql語句
		users = session.selectList("test.queryAll");
		// 提交事務
		// session.commit();
		// 關閉session
		session.close();
		return users;
	}

	@Override
	public int update(User user) {
		int result = -1;
		// 開啟session(建立一個sqlSession)
		SqlSession session = factory.openSession();
		// 通過session執行sql語句
		result = session.update("test.update", user);
		// 提交事務
		session.commit();
		// 關閉session
		session.close();
		return result;
	}

	@Override
	public int delete(int id) {
		int result = -1;
		// 開啟session(建立一個sqlSession)
		SqlSession session = factory.openSession();
		// 通過session執行sql語句
		result = session.delete("test.delete", id);
		// 提交事務
		session.commit();
		// 關閉session
		session.close();
		return result;
	}

}

MyBatisTest 測試:

package com.igeek.crm.test;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Before;
import org.junit.Test;

import com.igeek.crm.dao.UserDAO;
import com.igeek.crm.dao.impl.UserDAOImpl;
import com.igeek.crm.pojo.User;

/**
 * @author www.igeehome.com
 * 
 * TODO
 *
 * 2018年10月13日下午2:11:12
 */
public class MyBatisTest {
	SqlSessionFactory factory =null;
	private UserDAO userDao = null;
	@Before
	public void testInit(){
//		 1. 建立SqlSessionFactoryBuilder物件
		SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//		 2. 載入SqlMapConfig.xml配置檔案  /20181013_mybatis/config/SqlMapConfig.xml
		InputStream in = MyBatisTest.class.getResourceAsStream("/SqlMapConfig.xml");
//		 3. 建立SqlSessionFactory物件
		factory = builder.build(in);
		
		//建立dao物件
		userDao = new UserDAOImpl(factory);
	}
	
	@Test
	public void testDelete(){
		//先查詢
		int result = userDao.delete(32);
		System.out.println(result);
	}
	
	@Test
	public void testUpdate(){
		//先查詢
		User u = userDao.queryById(32);
		u.setUsername("趙六");
		int result = userDao.update(u);
		System.out.println(result);
	}
	
	
	@Test
	public void testQuerByName(){
		List<User> us = userDao.queryByName("小");
		System.out.println(us);
	}
	
	@Test
	public void testQuerById(){
		User u = userDao.queryById(32);
		System.out.println(u);
	}
	
	@Test
	public void testSave(){
		User user = new User();
		user.setUsername("週六");
		user.setBirthday(new Date());
		user.setSex(1);
		user.setAddress("江蘇無錫");
		userDao.save(user);
	}
}