1、新建父工程,删除src目录
2、新建子工程(maven的子模块)
此时的父工程的配置文件为:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!--父工程-->
<groupId>pers.zhb.study</groupId>
<artifactId>mybatis_study</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<modules>
<module>test_02</module>
</modules>
<!--依赖-->
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
</dependencies>
<!--在bulid中配置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>
</project>
子工程的配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>mybatis_study</artifactId>
<groupId>pers.zhb.study</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>test_02</artifactId>
<packaging>war</packaging>
<name>test_02 Maven Webapp</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
<build>
<finalName>test_02</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
<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>
</project>
这就体现出了分模块开发的好处:父模块导入依赖后子模块不需要重新导入,子模块继承父模块的依赖
注意:maven创建工程配置文件不生效问题:https://tech.souyunku.com/zhai1997/p/12770767.html
3、书写pojo
package pers.zhb.pojo;
import java.io.Serializable;
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
private String studentno;
private String sname;
private String sex;
private String birthday;
private String classno;
private String point;
private String phone;
private String email;
private Clas clas;
@Override
public String toString() {
return "Student{" +
"studentno='" + studentno + '\'' +
", sname='" + sname + '\'' +
", sex='" + sex + '\'' +
", birthday='" + birthday + '\'' +
", classno='" + classno + '\'' +
", point='" + point + '\'' +
", phone='" + phone + '\'' +
", email='" + email + '\'' +
", clas=" + clas +
'}';
}
public Clas getClas() {
return clas;
}
public void setClas(Clas clas) {
this.clas = clas;
}
public String getStudentno() {
return studentno;
}
public void setStudentno(String studentno) {
this.studentno = studentno;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
public String getClassno() {
return classno;
}
public void setClassno(String classno) {
this.classno = classno;
}
public String getPoint() {
return point;
}
public void setPoint(String point) {
this.point = point;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
4、编写mybatis的核心配置文件
<?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="jdbc.properties"/>
<environments default="development">
<environment id="development">
<!-- 使用jdbc事务管理 -->
<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>
<mapper resource="pers/zhb/dao/StudentMapper.xml"></mapper>
</mappers>
</configuration>
数据库的参数:
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/stu_mangement
jdbc.username=root
jdbc.password=root
注意:子配置文件需要被引入到核心配置文件
5、书写mybatis的工具类
package pers.zhb.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;
public class MybatisUtils{
private static SqlSessionFactory sqlSessionFactory;
static {
try{
//加载核心配置文件
String resource = "sqlMapConfig.xml";
InputStream in = Resources.getResourceAsStream(resource);
//创建SqlSessionFactory
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
}catch (IOException e){
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
return sqlSessionFactory.openSession();
}
}
SqlSessionFactoryBuilder:一旦创建了sqlSessionFactory就不再需要SqlSessionFactoryBuilder了
sqlSessionFactory:一旦被创建就会在应用的运行期间一直存在,不会对其它或者创建另一个实例
sqlsession:每一个线程都有自己的sqlSession实例,不是线程安全的不能被共享
6、书写dao层的接口,以及绑定该接口的配置文件
接口:
package pers.zhb.dao;
import pers.zhb.pojo.Student;
import java.util.List;
public interface StudentDao {
List<Student> getStudentList();
}
以上是为了理解,将报名和类名都写成了dao,在mybatis中一般写为mapper:
public interface StudentMapper {
List<Student> getStudentList();
}
配置文件:
<?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="pers.zhb.dao.StudentDao"><!--绑定一个dao接口-->
<select id="getStudentList" resultType="pers.zhb.pojo.Student">
select * from student
</select>
</mapper>
在不使用mybatis的时候,在书写dao层的接口后需要创建接口的实现类,通过执行sql语句来实现对数据库的操作。然而,在mybatis中实现类的功能被配置文件代替了。
7、测试
方式一:
package pers.zhb.test;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pers.zhb.dao.StudentDao;
import pers.zhb.pojo.Student;
import pers.zhb.utils.MybatisUtils;
import java.util.List;
public class MyTest {
@Test
public void test(){
SqlSession sqlSession= MybatisUtils.getSqlSession();
StudentDao studentDao=sqlSession.getMapper(StudentDao.class);
List<Student> students=studentDao.getStudentList();
for(Student student:students){
System.out.println(student);
}
sqlSession.close();
}
}
Student{studentno='201814', sname='zhai3', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null}
Student{studentno='201815', sname='qwerr', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null}
Student{studentno='201816', sname='jiayou', sex='男', birthday='1998-11-11', classno='80501', point='892', phone='19837372534', email='null', clas=null}
Student{studentno='201817', sname='null', sex='null', birthday='null', classno='2', point='null', phone='null', email='null', clas=null}
Student{studentno='201818', sname='null', sex='null', birthday='null', classno='2', point='null', phone='null', email='null', clas=null}
方式二(不推荐):
package pers.zhb.test;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;
import pers.zhb.pojo.Student;
import pers.zhb.utils.MybatisUtils;
import java.util.List;
public class MyTest {
@Test
public void test(){
SqlSession sqlSession= MybatisUtils.getSqlSession();
List<Student> students=sqlSession.selectList("pers.zhb.dao.StudentDao.getStudentList");
for(Student student:students){
System.out.println(student);
}
sqlSession.close();
}
}
8、项目结构