1.MyBatis概述
1.1 ORM框架
对象关系映射(Java中的对象 对应 数据库中的表)
1.2 官网地址
mybatis
1.3 MyBatis Plus
MyBatis-Plus (baomidou.com)官网
1.4 JPA
Java 持久层API
2. MAVEN引入
2.1 配置maven
2.2 导入MyBatis
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
2.3 MySQL driver
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
2.4 建立目录com/itheima/mapper
2.4.1 创建mapper接口和xml文件
note:因为此时为入门所以放在一块后续则不然
2.5 配置resources
2.5.1 配置db.properties
db.driver=com.mysql.cj.jdbc.Driver
db.url=jdbc:mysql:/db(数据库名)
db.username=//自己的用户名
db.password=//密码
2.5.2 src/main/resocurces文件夹下创建mybatis-config.xml/mybatis-conf.xml
建议直接复制 需要注意 里面的映射关系需要修改
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties resource="db.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${db.driver}"/>
<property name="url" value="${db.url}"/>
<property name="username" value="${db.username}"/>
<property name="password" value="${db.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<package name="com/itheima/mapper"/>
</mappers>
</configuration>
2.5.3 日志文件log4j2
(1)导入依赖
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
(2)配置log4j2.xml文件
note:这里我的包名为com 也就是src/main/java/com 如果是别的需要修改对应的名字下方会有标注
log4j2.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="LogToConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
<File name="LogToFile" fileName="log/test.log">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<!-- 注意包名 这里为com -->
<Logger name="com" level="error">
<AppenderRef ref="LogToConsole"/>
</Logger>
<!-- level 自己查 默认为error -->
<Logger name="com" level="debug" additivity="false">
<!-- 分别为打印再控制台和log.txt文件中 -->
<AppenderRef ref="LogToConsole"/>
<AppenderRef ref="LogToFile"/>
</Logger>
<!-- 全局配置文件 无论哪有错误都会输出再控制台 -->
<Root level="error">
<AppenderRef ref="LogToConsole"/>
</Root>
</Loggers>
</Configuration>
整体项目结构如图(log4j2.xml 为日志文件)
2.6 编写XxxMapper和XxxMapper.xml
2.6.1 映射编写sql
Xxmapper
public interface DbMapper {
String selectversion();
}
Xxmapper.xml
2.6.3 注意事项
因为xml文件和mapper接口在java包下系统不会自动编译其下的xml文件需要pom.xml文件中添加buder
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
<include>**/*.html</include>
</includes>
</resource>
</resources>
</build>
因为添加之后resources文件也有可能不会自动编译建议加上
是否编译可以看一下打包后的target/classes文件夹下是否有这个xml文件
2.7 编写测试代码
在test目录下编写测试代码 这一种需要配置mybatis-conf.xml文件链接数据库
public class Demo {
public static void main(String[] args) {
try (InputStream is = Resources.getResourceAsStream("mybatis-conf.xml")) {
//建立会话工厂
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = build.openSession();
DbMapper mapper = sqlSession.getMapper(DbMapper.class);
Book book = new Book();
book.setPrice(10.0D);
book.setNumber(15);
book.setBname("《斗feng大陆》");
book.setCratedate(LocalDateTime.now());
System.out.println(mapper.add(book));
sqlSession.commit();
System.out.println(mapper.selectAll());
System.out.println(mapper.selectversion());
System.out.println(mapper.dbs());
} catch (Exception e) {
e.printStackTrace();
}
}
}
也可以直接在代码中链接数据库 但是大部分时间基本不用
public class Deno {
public static void main(String[] args) {
PooledDataSource ds = new PooledDataSource("com.mysql.cj.jdbc.Driver","jdbc:mysql://localhost:3306/mybatis","root","");
TransactionFactory ts = new JdbcTransactionFactory();
Environment env = new Environment("dev",ts,ds);
Configuration cfg = new Configuration(env);
cfg.addMapper(DbMapper.class);
SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);
SqlSession ss = sf.openSession();
DbMapper mapper = ss.getMapper(DbMapper.class);
System.out.println(mapper.selectversion());
System.out.println(mapper.dbs());
System.out.println(mapper.selectAll());
}
}
2.8 结果
……………………………………day01…………………………………………