MyBatis对象分析
package com.example.test;
import com.example.pojo.Student;
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.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestStudent {
@Test
public void testGetAll() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
List list = sqlSession.selectList("wangxun.getAll");
list.forEach(System.out::println);
sqlSession.close();
}
@Test
public void testGetById() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
Student student = sqlSession.selectOne("wangxun.getById", 3);
System.out.println(student);
sqlSession.close();
}
@Test
public void testGetByName() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
List students = sqlSession.selectList("wangxun.getByName", "三");
students.forEach(System.out::println);
sqlSession.close();
}
@Test
public void testInsert() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
int num = sqlSession.insert("wangxun.insert", new Student("小涵", "0321@qq.com", 20));
sqlSession.commit();
if (num == 1) {
System.out.println("插入成功!");
}else{
System.out.println("插入失败!");
}
sqlSession.close();
}
@Test
public void testDelete() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
int num = sqlSession.delete("wangxun.delete", 5);
sqlSession.commit();
if(num == 1){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
sqlSession.close();
}
@Test
public void testUpdate() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
SqlSession sqlSession = factory.openSession();
int num = sqlSession.update("wangxun.update", new Student(1, "小何", "hehe@qq.com", 20));
sqlSession.commit();
if(num == 1){
System.out.println("更新成功!");
}else{
System.out.println("更新失败!");
}
sqlSession.close();
}
}
- Resources类
- 解析SqlMapConfig,xml文件,创建出相应的对象
- InputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
- SqlSessionFactory接口
- DefaultSqlSessionFactory是其一个实现类
- ctrl + h 可以快捷查看本接口的子接口及其实现类
- SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
- SqlSession接口
测试代码简化
package com.example.test;
import com.example.pojo.Student;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestStudent {
SqlSession sqlSession;
@Before
public void createSqlSession() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
sqlSession = factory.openSession();
}
@After
public void closeSqlSession(){
sqlSession.close();
}
@Test
public void testGetAll() throws IOException {
List list = sqlSession.selectList("wangxun.getAll");
list.forEach(System.out::println);
}
@Test
public void testGetById() throws IOException {
Student student = sqlSession.selectOne("wangxun.getById", 3);
System.out.println(student);
}
@Test
public void testGetByName() throws IOException {
List students = sqlSession.selectList("wangxun.getByName", "三");
students.forEach(System.out::println);
}
@Test
public void testInsert() throws IOException {
int num = sqlSession.insert("wangxun.insert", new Student("小涵", "0321@qq.com", 20));
sqlSession.commit();
if (num == 1) {
System.out.println("插入成功!");
}else{
System.out.println("插入失败!");
}
}
@Test
public void testDelete() throws IOException {
int num = sqlSession.delete("wangxun.delete", 5);
sqlSession.commit();
if(num == 1){
System.out.println("删除成功!");
}else{
System.out.println("删除失败!");
}
}
@Test
public void testUpdate() throws IOException {
int num = sqlSession.update("wangxun.update", new Student(1, "小何", "hehe@qq.com", 20));
sqlSession.commit();
if(num == 1){
System.out.println("更新成功!");
}else{
System.out.println("更新失败!");
}
}
}
实体类别名注册
<typeAliases>
<typeAlias type="com.example.pojo.Student" alias="student"/>
typeAliases>
<typeAliases>
<package name="com.example.pojo"/>
typeAliases>
<select id="getAll" resultType="student">
select
id, name, email, age
from
student
select>
设置日志输出
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
settings>
package com.example.test;
import com.example.pojo.Student;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class TestStudent {
SqlSession sqlSession;
@Before
public void createSqlSession() throws IOException {
InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
sqlSession = factory.openSession();
}
@After
public void closeSqlSession(){
sqlSession.close();
}
@Test
public void testGetAll() throws IOException {
List list = sqlSession.selectList("wangxun.getAll");
list.forEach(System.out::println);
}
}