mybaits 的配置文件
<?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"/>
<typeAliases>
<package name="note.entity"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="note.dao"/>
</mappers>
</configuration>
数据库的 properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/stage2?useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=123
log4j 的配置 properties
log4j.rootLogger=OFF
log4j.logger.note.dao=DEBUG,CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%p %c{1}:%L - %m%n
log4j.appender.FILE=org.apache.log4j.DailyRollingFileAppender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%p] %m [%t] %c [%l]%n
log4j.appender.FILE.File=file.log
生成 sqlssesion 的工具类
package note.util;
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 java.io.IOException;
import java.io.InputStream;
public class SqlSessionUtil {
static SqlSessionFactory sqlSessionFactory= null;
public static SqlSession getSqlSession(){
if (sqlSessionFactory == null) {
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream("mybatisConfig.xml");
} catch (IOException e) {
e.printStackTrace();
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
return sqlSessionFactory.openSession(false);
}
}
entity 实体类
package note.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private Integer id;
private String name;
private Boolean gender;
private Date birthday;
public Student(String name,Boolean gender,Date birthday) {
this.name = name;
this.gender = gender;
this.birthday = birthday;
}
}
package note.entity;
import lombok.Data;
import java.util.List;
@Data
public class Page {
private int pageNum;
private int pageSize;
private int pages;
private int total;
private List<Student> data;
}
dao 接口
package note.dao;
import note.entity.Page;
import note.entity.Student;
import java.util.List;
public interface StudentDao {
List<Student> selectStudentByIf(Student student);
List<Student> selectStudentByWhere(Student student);
List<Student> selectStudentByChoose(Student student);
List<Student> selectStudentByArray(int[] ints);
Student selectStudentByFragment(int id);
boolean updateStudentByIf(Student student);
List<Student> selectByPage(Page page);
List<Student> selectStudentByLt();
List<Student> selectStudentByCdata();
}
接口的 mapper
<?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="note.dao.StudentDao">
<select id="selectStudentByIf" resultType="note.entity.Student">
select * from student where 1 = 1
<if test="id != null and id != ''">
and id like '%' #{id} '%'
</if>
<if test="name != null and name != ''">
and name like '%' #{name} '%'
</if>
<if test="gender != null and gender != ''">
and gender like '%' #{gender} '%'
</if>
</select>
<select id="selectStudentByWhere" resultType="note.entity.Student">
select * from student
<where>
<if test="id != null and id != ''">
and id=#{id}
</if>
<if test="name != null and name != ''">
and name like '%' #{name} '%'
</if>
<if test="gender != null and gender != ''">
and gender=#{gender}
</if>
</where>
</select>
<select id="selectStudentByChoose" resultType="note.entity.Student">
select * from student
<where>
<choose>
<when test="id != null and id != ''">
and id=#{id}
</when>
<when test="name != null and name != ''">
and name like '%' #{name} '%'
</when>
<when test="gender != null and gender != ''">
and gender=#{gender}
</when>
<otherwise>
and 1 = !1
</otherwise>
</choose>
</where>
</select>
<select id="selectStudentByArray" resultType="note.entity.Student">
select * from student
<if test="array != null and array.length !=0">
where id in
<foreach collection="array" item="id" open="(" close=")" separator=",">
#{id}
</foreach>
</if>
</select>
<select id="selectStudentByFragment" resultType="note.entity.Student">
select <include refid="res"/> from student where id=#{id}
</select>
<sql id="res">
id,name,gender,birthday
</sql>
<update id="updateStudentByIf">
update student set
<if test="name != null and name != ''">
name=#{name}
</if>
<if test="gender != null">
and gender=#{gender}
</if>
<if test="birthday != null">
and birthday=#{birthday}
</if>
where id=#{id}
</update>
<select id="selectByPage" resultType="Student">
<bind name="startRow" value="(pageNum - 1) * pageSize"/>
select * from student limit #{startRow},#{pageSize}
</select>
<select id="selectStudentByLt" resultType="note.entity.Student">
select *
from student where id < 5
</select>
<select id="selectStudentByCdata" resultType="note.entity.Student">
select *
from student where id <![CDATA[ < ]]> 5 </select>
</mapper>
测试类
package note.dao;
import note.entity.Page;
import note.entity.Student;
import note.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import java.util.List;
public class StudentDaoTest {
private SqlSession sqlSession = SqlSessionUtil.getSqlSession();
private StudentDao studentDao = sqlSession.getMapper(StudentDao.class);
@Test
public void selectStudentByIf() {
Student student = new Student();
student.setName("冬梅");
List<Student> students = studentDao.selectStudentByIf(student);
System.out.println("students = " + students);
sqlSession.close();
}
@Test
public void selectStudentByWhere() {
Student student = new Student();
student.setName("张");
student.setGender(false);
List<Student> students = studentDao.selectStudentByWhere(student);
System.out.println("students = " + students);
sqlSession.close();
}
@Test
public void selectStudentByChoose() {
Student student = new Student();
student.setName("张");
student.setId(2);
List<Student> students = studentDao.selectStudentByChoose(student);
System.out.println("students = " + students);
sqlSession.close();
}
@Test
public void selectStudentByArray() {
List<Student> students = studentDao.selectStudentByArray(new int[]{1, 3, 5});
System.out.println(students);
}
@Test
public void selectStudentByFragment() {
Student student = studentDao.selectStudentByFragment(1);
System.out.println(student);
}
@Test
public void updateStudentByIf() {
Student student = new Student();
student.setId(1);
student.setName("noby1");
boolean b = studentDao.updateStudentByIf(student);
System.out.println(b);
sqlSession.close();
}
@Test
public void testSelectRangeBirthdayByEntity() {
Page page = new Page();
page.setPageNum(1);
page.setPageSize(4);
List<Student> students = studentDao.selectByPage(page);
System.out.println("students = " + students);
sqlSession.close();
}
@Test
public void selectStudentByLt() {
List<Student> students = studentDao.selectStudentByLt();
System.out.println("students = " + students);
}
@Test
public void selectStudentByCdata() {
List<Student> students = studentDao.selectStudentByCdata();
System.out.println("students = " + students);
}
}