MyBatis解决了JDBC的不便于修改,硬编码过多的缺点,是一种比较轻量级的框架
动态代理模式能简化代码的编写,只需配置一些xml文件就能动态生成代理对象来执行sql。
需要的文件:JDK、Mybatis架包(官网下载后lib全部导入)、log4日志文件(可以不用)、JDBC驱动包(网上哪都有)
关键文件:sqlMapConfig.xml(差不多是个数据库全局配置,需放到classpath下)、类+Mapper.xml(sql语句相关配置文件)、jdbc.properties(存放JDBC连接参数)
创建一个java工程如图所示:注意sqlMapConfig.xml文件一定要放到classpath下,只需将文件夹右键Sourse Folder就行
非代理模式:在xml中配置sql并按id区分,SqlSession再跟进namespace+id来执行不同的sql
入门程序配置如下
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>
<!-- 加载数据库properties文件 -->
<properties resource="jdbc.properties"></properties>
<!-- 配置别称,Mybatis已经默认配置一些了比如 _int -->
<typeAliases>
<typeAlias type="com.baidu.dept.Dept" alias="dept"/>
</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>
<!-- 关联Mapper.xml文件以用来执行sql语句 -->
<mappers>
<!-- 这样写是非代理模式下 -->
<!-- <mapper resource="com/baidu/mapper/DeptMapper.xml"/> -->
<!-- 这样写是代理模式-->
<package name="com.baidu.mapper"/>
</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以用来控制SQL语句 -->
<mapper namespace="test">
<select id="getDeptList" resultType="dept">
select * from testdept
</select>
<!-- 加上#表示使用parperment即预编译,使用$表示的statment也就是拼接字符串 -->
<select id="getDeptById" parameterType="int" resultType="dept">
select * from testdept where did=#{did}
</select>
<select id="getDeptById2" parameterType="int" resultType="dept" >
select * from testdept where did=${value}
</select>
<select id="getDeptListByMohu" parameterType="String" resultType="dept">
select * from testdept where dname like #{dname}
</select>
<select id="getDeptListByMohu2" parameterType="String" resultType="dept">
select * from testdept where dname like ${value}
</select>
<!-- #后面写什么都行,但是如果参数是个对象,就要和对对象里的 变量写法一样-->
<select id="getDeptListByDept" parameterType="com.baidu.dept.Dept" resultType="dept">
select * from testdept where dname like #{dName}
</select>
<select id="getDeptCount" resultType="int">
select count(did) from testdept
</select>
<insert id="insertDept" parameterType="dept">
insert into testdept set dname=#{dName}
</insert>
<insert id="insertDept2" parameterType="dept">
<!-- 返回主键 -->
<!-- 这里传进来的是对象,所以判断的时候以对象的dId -->
<selectKey keyProperty="dId" resultType="int" order="AFTER">
select LAST_INSERT_ID()
</selectKey>
insert into testdept set dname=#{dName}
</insert>
<delete id="delDeptById" parameterType="dept">
delete from testdept where did=#{dId}
</delete>
<update id="updateDeptById" parameterType="dept">
update testdept set dname=#{dName} where did=#{dId}
</update>
</mapper>
测试文件
package com.baidu.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.baidu.dept.Dept;
public class Test1 {
private InputStream inputStream;
SqlSessionFactory sessionFactory;
SqlSession session;
@Before
public void Init() throws IOException {
inputStream=Resources.getResourceAsStream("sqlMapConfig.xml");
sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
session = sessionFactory.openSession();
}
@Test
public void test1() {
List<Dept> deptList = session.selectList("test.getDeptList");
Dept dept = session.selectOne("test.getDeptById",1);
System.out.println(deptList);
System.out.println(dept);
}
@Test
public void test2() {
List<Dept> deptList = session.selectList("test.getDeptListByMohu","%雄%");
System.out.println(deptList);
}
@Test
public void test3() {
List<Dept> deptList = session.selectList("test.getDeptListByDept",new Dept(2, "呵呵哒"));
System.out.println(deptList);
}
@Test
public void test4() {
Integer i = session.selectOne("test.getDeptCount");
System.out.println(i);
}
@Test
public void test5() {
//session是默认不会自动提交事务操作的,需要手动提交事务
Dept dept=new Dept(null, "呵呵哒");
int i=session.insert("test.insertDept2", dept);
session.commit();
System.out.println(dept);
}
@Test
public void test6() {
session.delete("test.delDeptById",new Dept(9, "?!"));
session.commit();
}
@Test
public void test7() {
session.delete("test.updateDeptById",new Dept(8, "老王"));
session.commit();
}
@After
public void closeSession() {
session.close();
}
}
需要注意:参数是对象时,需要注意#中的值要和对象里的值一样
代理模式:通过动态代理一个接口来动态创建接口代理对象
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以用来控制SQL语句 -->
<mapper namespace="com.baidu.mapper.DeptMapper">
<!-- 使用代理模式....................................................... -->
<select id="getDeptList" resultType="dept">
select * from testdept
</select>
<select id="getDeptListById" resultType="dept" parameterType="dept">
select * from testdept where did=#{dId}
</select>
<select id="getDeptListById2" resultType="dept" parameterType="dept">
select * from testdept
<where>
<if test="dId!=null">
and did=#{dId}
</if>
</where>
</select>
<select id="getDeptListById3" resultType="dept" parameterType="dept">
select * from testdept
<where>
and did
<if test="deptList!=null">
<foreach collection="deptList" open="in(" close=")" separator="," item="d">
#{d}
</foreach>
</if>
</where>
</select>
<sql id="getDeptSql">
<if test="dId!=null">
and did=#{dId}
</if>
</sql>
<select id="getDeptByDept" resultType="dept" parameterType="dept">
select * from testdept
<where> <include refid="getDeptSql"></include> </where>
</select>
</mapper>
测试文件:
package com.baidu.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.baidu.dept.Dept;
import com.baidu.mapper.DeptMapper;
public class Test1 {
private InputStream inputStream;
SqlSessionFactory sessionFactory;
SqlSession session;
@Before
public void init() throws IOException {
inputStream=Resources.getResourceAsStream("sqlMapConfig.xml");
sessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
session = sessionFactory.openSession();
}
//////////////////////////////////////////////////////////////////////////////////////////////////////
@Test
public void test8() {
DeptMapper mapper = session.getMapper(DeptMapper.class);
List<Dept> deptList = mapper.getDeptList();
System.out.println(deptList);
}
@Test
public void test9() {
DeptMapper mapper = session.getMapper(DeptMapper.class);
List<Dept> deptListById = mapper.getDeptListById(new Dept(1, null));
System.out.println(deptListById);
}
@Test
public void test10() {
DeptMapper mapper = session.getMapper(DeptMapper.class);
List<Dept> deptListById2 = mapper.getDeptListById2(new Dept(2,null));
System.out.println(deptListById2);
}
@Test
public void test11() {
DeptMapper mapper = session.getMapper(DeptMapper.class);
List<Integer> depts=new ArrayList<>();
depts.add(1);
depts.add(2);
depts.add(3);
Dept dept = new Dept(null, null);
dept.setDeptList(depts);
List<Dept> deptListById = mapper.getDeptListById3(dept);
System.out.println(deptListById);
}
@Test
public void test12() {
DeptMapper mapper = session.getMapper(DeptMapper.class);
Dept deptByDept = mapper.getDeptByDept(new Dept(1, null));
System.out.println(deptByDept);
}
@After
public void closeSession() {
session.close();
}
}