一、在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();
}
}