目录
1.MyBatis支持的事务管理器:JDBC,MANAGED(托管)
2.MyBatis配置数据库
3.MyBatis查询
4.MyBatis动态SQL
5.MyBatis杂项
6.MyBatis分页查询
7.MyBatis缓存
8.使用注解配置 SQL 映射器
1.MyBatis支持的事务管理器:JDBC,MANAGED(托管)
1.JDBC 应用程序负责管理数据库连接的生命周期
2.MANAGED 由应用服务器负责管理数据库连接的生命周期(一般用于商业服务器JBoss,WebLogic)
2.MyBatis配置数据库
1.UNPOOLED 没有数据库连接池
2.POOLED 数据库连接池,配置jdbc.properties文件
jdbc.driverClassName=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/zhaoyh01 jdbc.username=root jdbc.password=root
3.JNDI 使用应用服务器配置JNDI数据源获取数据库链接
mybatis-config.xml配置文件
3.MyBatis查询
1.association 一对一查询
2.collection 一对多查询
4.MyBatis动态SQL
1. if 标签
2. choose,when,otherwise ,类似与Java 中的 switch 语句
3. where 自动加上where,如果where字句以and或者or开头,则自动删除第的一个and或者or;
4.trim
常用的属性:
prefix=”where”//给第一个符合条件的语句 加上前缀where
prefixOverrides=”and/or” //去掉第一个and或者是or
suffix=”and” //给最后一个符合条件的语句 加上后缀 and
suffixOverrides=”and”//去掉最后一条语句的后缀 and
5.foreach 循环
常用的属性:
item 要遍历的元素;
collection 要遍历的集合;
index 元素在集合中的索引;
open 遍历以什么开头,例 open=” (“;
seprator 遍历出来的元素以什么分隔;
end 遍历以什么结束 end=”)”
6. set 条件 自动加上set,自动删除最后一个逗号','
update t_student where id=#{id} name=#{name}, age=#{age},
5.MyBatis杂项
1.处理CLOB、BLOB类型数据
student.java类
package com.zhaoyh.model;
public class Student {
private Integer id;
private String name;
private Integer age;
private byte[] picture;//BLOB类型的(MySql中的longblob)
private String remark;//CLOB类型(MySql中的longtext)
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public byte[] getPicture() {
return picture;
}
public void setPicture(byte[] picture) {
this.picture = picture;
}
public String getRemark() {
return remark;
}
public void setRemark(String remark) {
this.remark = remark;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", remark=" + remark + "]";
}
}
student接口方法
package com.zhaoyh.mappers;
import java.util.List;
import java.util.Map;
import com.zhaoyh.model.Student;
public interface StudentMapper {
//处理Clob/Blob类型的数据
public int insertStudent(Student student);
public Student getStudentById(Integer id);
}
mapper映射文件
insert into t_student values(null,#{name},#{age},'1','1',#{picture},#{remark})
junittest测试类
public class StudentServiceTest {
private static Logger log = Logger.getLogger(StudentService.class);
private SqlSession sqlSession = null;
private StudentMapper studentMapper = null;
@Before
public void setUp() throws Exception {
sqlSession = SqlSessionFactoryUtil.openSession();
studentMapper = sqlSession.getMapper(StudentMapper.class);
}
@After
public void tearDown() throws Exception {
sqlSession.close();
}
@Test
public void testInsertStudent() {
log.info("使用@Insert注解方式添加学生信息...");
Student student = new Student("test03",22);
studentMapper.insertStudent(student);
sqlSession.commit();
}
@Test
public void testUpdateStudent(){
log.info("使用@Update注解方式修改学生信息");
Student student = new Student(12,"test032",25);
studentMapper.updateStudent(student);
sqlSession.commit();
}
@Test
public void testDeleteStudentById(){
log.info("使用@Delete注解方式删除学生信息");
studentMapper.deleteStudentById(13);
sqlSession.commit();
}
@Test
public void testGetStudentById(){
log.info("使用@Select注解方式查询学生信息");
Student student = studentMapper.getStudentById(12);
System.out.println("Student="+student);
}
@Test
public void testFindStudents(){
log.info("使用@Select注解方式查询学生信息");
List studentList = studentMapper.findStudents();
for(Student student:studentList){
System.out.println("Student="+student);
}
}
}
9.关系映射:一对一映射
一个学生信息对应一个地址信息
student.java
package com.zhaoyh.model;
public class Student {
private Integer id;
private String name;
private Integer age;
private Address address;//地址信息
public Student() {
super();
// TODO Auto-generated constructor stub
}
public Student(Integer id, String name, Integer age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Student(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + ", address=" + address + "]";
}
}
Address.java
package com.zhaoyh.model;
public class Address {
private Integer id;
private String sheng;
private String shi;
private String qu;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getSheng() {
return sheng;
}
public void setSheng(String sheng) {
this.sheng = sheng;
}
public String getShi() {
return shi;
}
public void setShi(String shi) {
this.shi = shi;
}
public String getQu() {
return qu;
}
public void setQu(String qu) {
this.qu = qu;
}
@Override
public String toString() {
return "Address [id=" + id + ", sheng=" + sheng + ", shi=" + shi + ", qu=" + qu + "]";
}
}
Student接口方法
package com.zhaoyh.mappers;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.zhaoyh.model.Student;
public interface StudentMapper {
@Select("select * from t_student where id=#{id}")
@Results({
@Result(id=true,column="id",property="id"),
@Result(column="name",property="name"),
@Result(column="age",property="age"),
@Result(column="addressId",property="address",one=@One(select="com.zhaoyh.mappers.AddressMapper.findById"))
})
public Student findStudentWithAddress(int id);
}
Address接口方法
package com.zhaoyh.mappers;
import org.apache.ibatis.annotations.Select;
import com.zhaoyh.model.Address;
public interface AddressMapper {
@Select("select * from t_address where id=#{id}")
public Address findById(Integer id);//根据主键查询
}
JUnitTest测试类
package com.zhaoyh.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.zhaoyh.mappers.StudentMapper;
import com.zhaoyh.model.Student;
import com.zhaoyh.service.StudentService;
import com.zhaoyh.util.SqlSessionFactoryUtil;
public class StudentServiceTest {
private static Logger log = Logger.getLogger(StudentService.class);
private SqlSession sqlSession = null;
private StudentMapper studentMapper = null;
@Before
public void setUp() throws Exception {
sqlSession = SqlSessionFactoryUtil.openSession();
studentMapper = sqlSession.getMapper(StudentMapper.class);
}
@After
public void tearDown() throws Exception {
sqlSession.close();
}
@Test
public void testFindStudentWithAddress(){
log.info("一对一映射:学生信息包含地址信息...");
Student student = studentMapper.findStudentWithAddress(2);
System.out.println("Student="+student);
}
}



