- 开发准备细节分析IDEA 模板创建工具类mybaits文档
创建maven 文件
pom 文件导入依赖:
org.mybatis mybatis 3.5.9 mysql mysql-connector-java 8.0.28 log4j log4j 1.2.17 junit junit 4.13.1 test src/main/java ***.xml false
- 创建日志文件,可不创:
log4j.rootLogger = debug,console
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern = %d{yyyy-MM-dd HH:mm:ss} % -5p %c{1}:%L -%m%n
- 创建mybatis mapper文件:
- 创建DAO 接口 ,每一个接口一个mapper文件来操作数据库
package dao;
import domain.Student;
import java.util.List;
public interface StudentDao {
//查询一个学生
Student selectStudentById(Integer id);
int insertStudent(Student student); //int 表示添加的函数
List selectStudents();
}
- 对应的mapper文件,一般创建在同一个文件夹下。
- 创建数据库类:student
package domain;
public class Student {
private Integer id;
private String name;
private Integer age;
private String email;
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 String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + ''' +
", age=" + age +
", email='" + email + ''' +
'}';
}
}
- 使用工具如navicat 创建数据库
id int pk,name varchar(255),email varchar(255),age int;测试 数据库的连接
import domain.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 utils.MyBatisUtil;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class MyTest {
@Test
public void testSlectStudentById() throws IOException {
//调用mybatis中的sql方法
String config = "mybatis.xml";
//获取输入流
InputStream inputStream = Resources.getResourceAsStream(config);
//使用SQLSessionFactoryBuilder 创建 sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
try (SqlSession session = sqlSessionFactory.openSession()) {
// s = namespace+'.'+"select|delele|update|insert" 标签的id属性值
String sqlid = "dao.StudentDao"+"."+"selectStudentById";
Student student = (Student) session.selectOne(sqlid,"1001");
// 占位符:传递给 #{xxx}
System.out.println("student = " + student);
session.close();
}
}
@Test
public void testInsert() throws IOException {
//调用mybatis中的sql方法
String config = "mybatis.xml";
//获取输入流
InputStream inputStream = Resources.getResourceAsStream(config);
//使用SQLSessionFactoryBuilder 创建 sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
try (SqlSession session = sqlSessionFactory.openSession()) {
// s = namespace+'.'+"select|delele|update|insert" 标签的id属性值
String sqlid = "dao.StudentDao"+"."+"insertStudent";
// 也可以在这里传入Student对象 需要在xml文件中使用#{属性名} 来获取属性的值。
Student student = new Student();
student.setAge(20);
student.setEmail("123456@qq.com");
student.setId(1003);
student.setName("wenjunze");
int rows = session.insert(sqlid,student);
// 占位符:传递给 #{xxx}
System.out.println("rows = " + rows);
//需要手工提交事务
session.commit();
session.close();
}
}
}
IDEA 创建模板文件:
使用工具类
package utils;
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;
// 工具类 创建SqlSession 对象
public class MyBatisUtil {
private static SqlSessionFactory factory = null;
static{
String config="mybatis.xml";
try {
InputStream inputStream = Resources.getResourceAsStream(config);
factory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//创建方法,获取Sqlsession 的对象
public static SqlSession getSqlSession(){
SqlSession session =null;
if(factory != null){
session = factory.openSession();
}
return session;
}
}
测试:
@Test
public void utilsTest() {
SqlSession session = MyBatisUtil.getSqlSession();
String sqid = "dao.StudentDao"+"."+"selectStudents";
List student = session.selectList(sqid);
for (Student student1 : student) {
System.out.println("student1 = " + student1);
}
session.close();
}



