定義別名簡化開發

一、在myBatisConfig.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="db.properties"></properties>
	<!-- 配置類的別名(配置完後,別名就是類名) 注意:這裏的配置不能放在後面,否則會出錯無效 -->
	<typeAliases>
		<package name="com.mingde.po"/>
	</typeAliases>
	<!-- 配置開發環境 -->
	<environments default="development">
		<environment id="development">
			<!-- 配置事務管理器 -->
			<transactionManager type="JDBC" />
			<!-- 配置數據源連接池 -->
			<dataSource type="POOLED">
				<property name="driver" value="${driver}"/>
				<property name="url" value="${url}"/>
				<property name="username" value="${username}"/>
				<property name="password" value="${password}"/>
			</dataSource>
		</environment>
	</environments>
	
	<!-- 配置sql語句映射文件-->
	<mappers>
		<mapper resource="com/mingde/mapper/Empmapper.xml" />
	</mappers> 
</configuration>


二、配置sql語句映射文件

<?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="test">
	<!-- 這裏的resultType 不用再寫全路徑了(com.mingde.Employee),而是直接寫該類的類名即可 -->
	<select id="seeAll" resultType="Employee">
		select * from Employee
	</select>
	<select id="seeAll2" resultMap="ResulstMap">
		select * from Employee
	</select>
	<resultMap type="Employee" id="ResulstMap">
		<id property="eid" column="id" />
		<result property="ename" column="name" />
	</resultMap>
	
	<!-- 添加數據(自動增長) -->
	<insert id="insert" parameterType="Employee">
		<selectKey keyProperty="eid" resultType="int" order="BEFORE">
			select seqstudent.nextval from dual
		</selectKey>
		insert into Employee values(#{eid},#{ename},#{sex},#{age},${birth})
	</insert>
	
	<!-- 模糊查詢 -->
	<select id="search" resultType="Employee" parameterType="String">
		select * from Employee where ename like '%${value}%'
	</select>
	<select id="search2" resultType="Employee" parameterType="String">
		select * from Employee where ename like #{value}
	</select>
	<select id="search3" resultType="Employee" parameterType="String">
		select * from Employee where ename like '%'||#{value}||'%'
	</select>
	
</mapper>


三、系統也給一些常用類定義了別名


  

四、配置MyBatis文件

package com.mingde.utils;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class MyBatisUtils {

	//獲取資源文件,創建SqlSessionFactory工廠
	public static SqlSessionFactory getSqlSessionFactory()throws Exception{
		InputStream inputStream = Resources.getResourceAsStream("myBatisConfig.xml");
		return new SqlSessionFactoryBuilder().build(inputStream);
	}
	//打開SqlSessoinFactory工廠,獲取Sqlsession
	public static SqlSession getSqlSession()throws Exception{
		return getSqlSessionFactory().openSession();
	}
	//打開SqlSessoinFactory工廠,獲取Sqlsession,並且自動提交commit;
	public static SqlSession getSqlSessionAutoCommit()throws Exception{
		//注意:如果爲true,則進增刪改時,會自動commit提交,如果不寫或爲false的話則不會自動commit提交,則需要在寫完增刪改之後,session.commmit();
		return getSqlSessionFactory().openSession(true);
	}
	
}

五、測試類

package com.mingde.test;

import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import com.mingde.dao.EmpDao;
import com.mingde.po.Employee;
import com.mingde.utils.MyBatisUtils;

public class TestEmp1 {

	@Test
	public void test() throws Exception {
		SqlSession sqlSession = MyBatisUtils.getSqlSession();
		List<Object> selectList = sqlSession.selectList("test.seeAll");
		List<Object> selectList2 = sqlSession.selectList("test.seeAll2");
		System.out.println(selectList);
		System.out.println(selectList2);
	}

	@Test
	public void test2() throws Exception {
		SqlSession sqlSession = MyBatisUtils.getSqlSession();
		sqlSession.insert("insert", new Employee( "酒九", "女", 15, "to_date('2016-6-6','yyyy-MM-dd')"));
		sqlSession.close();
	}
	
	@Test
	public void test3() throws Exception {
		SqlSession session = MyBatisUtils.getSqlSession();
		List<Object> selectList = session.selectList("test.search", "九");
		List<Object> selectList2 = session.selectList("test.search2", "%九%");
		List<Object> selectList3 = session.selectList("test.search3", "九");
		System.out.println(selectList);
		System.out.println(selectList2);
		System.out.println(selectList3);
		session.close();
	}
	
	
	
}

發佈了62 篇原創文章 · 獲贊 9 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章