一、环境配置
1.依赖配置
<!-- pom.xml -->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.26</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.32</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.7</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.31</version>
</dependency>
</dependencies>
2.数据库配置
<!-- resources/mappers/mybatis-config.xml -->
<environments default="dev">
<environment id="dev">
<transactionManager type="JDBC"></transactionManager>
<!--变更过的数据连接池,POOLED更改整合Druid-->
<dataSource type="com.imooc.dataSource.DruidDataSourceFactory">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/imooc-oa?useUnicode=true&characterEncoding=UTF8&serverTimeZone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
3.创建数据库连接工具类
package com.imooc.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.Reader;
import java.util.function.Function;
public class mybatisUtils {
private static SqlSessionFactory sqlSessionFactory = null;
static{
try {
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
} catch (IOException e) {
throw new ExceptionInInitializerError(e);
}
}
public static Object executeQuery(Function<SqlSession,Object> func){
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
Object obj = func.apply(sqlSession);
return obj;
} finally {
sqlSession.close();
}
}
}
4.创建Druid工具类
package com.imooc.dataSource;
import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.datasource.unpooled.UnpooledDataSourceFactory;
import javax.sql.DataSource;
import java.sql.SQLException;
public class DruidDataSourceFactory extends UnpooledDataSourceFactory {
public DruidDataSourceFactory() {
this.dataSource = new DruidDataSource();
}
public DataSource getDataSource() {
try {
((DruidDataSource) this.dataSource).init();
} catch (SQLException e) {
throw new RuntimeException(e);
}
return this.dataSource;
}
}
5. Maven项目增加Web模块
在项目配置里面Modules增加Web模块,修改path(将路径放到项目的src/main中),更名为webapp 。
在Facets中,修改开发描述和Web资源目录中的Path,具体和上一步目录地址相同。
6.配置Tomcat调试器
修改端口为浏览器默认端口80,方便调试。
Deployment增加Web项目, 应用上下文更改为 / ,方便调试。
7. 常用配置头
<!-- mybatis.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">
<!-- mappers.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">
二、具体实战
待续...