mybatis相应的配置流程如图所示:
(1)Pom依赖
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.19</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
(2)配置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>
<properties resource="db.properties"></properties>
<!-- 进行一些相应的设置,例如是否开启驼峰表示等等,可以参考官方文档-->
<!-- <settings>-->
<!-- <setting name="" value=""/>-->
<!-- </settings>-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<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="mapper.xml"></mapper>
</mappers>
</configuration>
(3)配置db.properties属性文件
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/demo?serverTimezone=UTC
jdbc.username=root
jdbc.password=root
(4)配置slf4g日志属性
# \u5168\u5C40\u65E5\u5FD7\u914D\u7F6E
log4j.rootLogger=DEBUG, stdout
# MyBatis \u65E5\u5FD7\u914D\u7F6E
log4j.logger.com.mashibing=TRACE
# \u63A7\u5236\u53F0\u8F93\u51FA
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
(5)编写实体类
public class User {
private Integer id;
private String name;
private String password;
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 String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
(6)mapper.xml文件配置
<?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="com.jyk.dao.UserDao">
<insert id="insert" parameterType="com.jyk.entity.User" >
insert into user(name,password) values(#{name},#{password})
</insert>
</mapper>
(7)编写dao层数据交互接口
public interface UserDao {
public Integer insert(User user);
}
(8)测试类编写
public class TestDemo {
@Test
public void testdemo() throws Exception {
String url ="mybatis-config.xml";
InputStream stream = Resources.getResourceAsStream(url);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
SqlSession sqlSession = sqlSessionFactory.openSession(true);
UserDao mapper = sqlSession.getMapper(UserDao.class);
User user = new User();
user.setName("root");
user.setPassword("root");
Integer insert = mapper.insert(user);
System.out.println(insert);
}
}
(9)执行结果
(10)总结
错误点:
(1)数据库连接信息,url需要填写时区信息
(2)mybatis-config配置文件加载mapper.xml文件可以使用多种方式,使用class的方式可以将xml文件放到对应接口文件夹中,但是需要使用maven编译,配置方式为
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
</build>
(3)需要将slf4g日志配置文件cpy到slf4j.properties文件中,logger方式为DEGUB,显示DEGUB详细信息
(4)mapper.xml文件中书写增删改查操作,在标签中需要添加很多的属性值,尤其select属性值比较多,可以参考官方文档来完成参数配置