Mybatis项目顺序
1. Mybatis开发流程
1. 搭建环境,搭建数据库
先创建数据库和数据表,然后进行连接
2. 引入Mybatis依赖
在pom.xml中添加三个依赖
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
3. 创建核心配置文件 (mybatis-config.xml)
(建议)在src/main/resources 目录下创建 mybatis-config.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>
<!--环境配置 default默认指向的数据库-->
<environments default="development">
<environment id="development">
<!--JDBC进行事务管理-->
<transactionManager type="JDBC"/>
<!--连接池对数据库进行管理-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/students?useSSL=true&useUnicode=true&characterEncoding=UTF-8"/>
<!--useSSL=true&可以不写,有事会出异常-->
<property name="username" value="root"/>
<property name="password" value="myt12345"/>
</dataSource>
</environment>
</environments>
</configuration>
4. 创建实体类
Student.java
public class Student {
private int id;
private int reg_no;
private String name;
private String sex;
private int age;
private String grade;
private String major;
public Student() {
}
public Student(int id, int reg_no, String name, String sex, int age, String grade, String major) {
this.id = id;
this.reg_no = reg_no;
this.name = name;
this.sex = sex;
this.age = age;
this.grade = grade;
this.major = major;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getReg_no() {
return reg_no;
}
public void setReg_no(int reg_no) {
this.reg_no = reg_no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGrade() {
return grade;
}
public void setGrade(String grade) {
this.grade = grade;
}
public String getMajor() {
return major;
}
public void setMajor(String major) {
this.major = major;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", reg_no=" + reg_no +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", grade='" + grade + '\'' +
", major='" + major + '\'' +
'}';
}
}
5. 创建一个接口 StudentDao/StudentMapper
public interface StudentMapper {
List<Student> selectAll();
}
6. 创建Mapper映射文件
student.xml 对应于实体类(建议创建在 src/main/resources 下)
<?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">
<!--namespace=绑定一个对应的,mapper接口-->
<!--id对应接口中的方法名-->
<!-- resultType返回一个结果,类型(全限定名) 相当于接口实现类中的结果集rs -->
<mapper namespace="com.tao.mybatis.domain.StudentMapper">
<select id="selectAll" resultType="com.tao.mybatis.domain.Student">
select * from students.student
</select>
</mapper>
在mybatis-config.xml配置文件中,添加映射文件
<mappers>
<mapper resource="student.xml"/> <!--此路径在 src/main/resources-->
</mappers>
7. 初始化SessionFactory
MybatisUtils工具类的抽取
/**
* MybatisUtils工具类,创建全局唯一的SqlSessionFactory对象
*/
public class MybatisUtils {
//static属于类,不属于对象,而且全局唯一
private static SqlSessionFactory sqlSessionFactory= null;
static {
Reader reader = null;
try {
reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
e.printStackTrace();
//初始化错误时,抛出异常
throw new ExceptionInInitializerError(e);
}
}
/**
* openSqlsession 创建一个新的SqlSession对象
* @return SqlSession
*/
public static SqlSession openSqlsession(){
return sqlSessionFactory.openSession();
}
/**
* 释放一个有效的SqlSession对象
* @param session 准备释放SqlSession对象
*/
public static void closeSession(SqlSession session){
if(session != null){
session.close();
}
}
}
8. 利用SqlSession对象操作数据
创建Test测试文件
@Test
public void test2() throws Exception {
SqlSession sqlSession = null;
try{
sqlSession = MybatisUtils.openSqlsession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> studentList = mapper.selectAll();
for (Student student : studentList) {
System.out.println(student);
}
}catch (Exception e){
throw e;
}finally {
MybatisUtils.closeSession(sqlSession);
}
}
注意点
- maven由于它的约定大于配置,我们之后可以能遇到我们写的配置文件,无法被导出或者生效的问题,解决方案:
放在pom.xml中,建议主工程和子工程都放 (建议在开始写xml文件 和 properties文件时,就写在 resources 下)
<!--在build中配置resources,来防止资源导出失败的问题-->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
-
驼峰命名的转换
在mybatis-config.xml 中,添加 settings 标签
<settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings>