1》pom文件配置build-resources,解决有时候出现父项目的依赖不能被子项目继承,或者一些file文件不能打包进target的情况
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
2》子项目pom文件配置parents,有时候父项目pom文件没有删除一些配置会导致子项目pom文件不能继承父项目pom配置。这时候可以手动给子项目添加parents标签。如下
<parent>
<groupId>org.example</groupId>
<artifactId>父项目名。如:javaweb02-servlet</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>子项目名。如:servlet.02</artifactId>
<packaging>war</packaging>
3》maven开发JavaWEB配置WEB-INF文件下web.xml Tomcat配置。此处的配置根据自己tomcat安装路径下D:\Environment\apache-tomcat-9.0.60\webapps\ROOT\WEB-INF的web.xml自行配置
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0"
metadata-complete="true">
</web-app>
4》resources文件下配置db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306?useSSL=false&useCharacterEncoding=utf-8jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
username=root
password=1227
5》Mybatis核心配置。
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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/mybatis/example/BlogMapper.xml"/>
</mappers>
</configuration>
6》mybatis配合工具类utils文件 MybatisUtils.java
可以直接使用 MybatisUtils.getSqlSession( ) 方法执行sql语句
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 = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
public static SqlSession getSqlSession(){
SqlSession sqlSession = sqlSessionFactory.openSession();
return sqlSession;
}
}
7》resources/mapper文件下配置各Dao层Mapper映射
此处的映射为Mybatis官网给出的模板,可以根据需求自行更改具体配置。如 resultType-resultMap, sql语句等等
<?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="dao需要操作的java文件路径。如com.flyfish.dao.TestMapper01">
<select id="方法名" resultType="返回结果类型">
select * from Blog where id = #{id}
</select>
</mapper>
**记得需要在核心配置mybatis-config.xml注册mapper/****.xml.
8》配置事务日志
此处最好从官网复制,不然容易配置错误导致报错
其中常用STDOUT_LOGGING和LOG4J。注意deprecated since 3.5.9(log4j是从mybatis3.5.9版本开始才可以使用)
例如配置STDOUT_LOGGING如下
在mybatis-config.xml配置
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
***注意mybatis.config.xml配置的顺序,顺序不正确会配置失败导致报错。“约定大于配置”
配置log4j时候我们需要先导入jar,从maven官网可以自行查找,不要下错就好
maven导入依赖后需要在resources配置log4j.properties文件,具体配置信息如下。具体log生成路径及日志大小根据需要自行调成
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码
log4j.rootLogger=DEBUG,console,file
#控制台输出的相关设置
log4j.appender.console = org.apache.log4j.ConsoleAppender
log4j.appender.console.Target = System.out
log4j.appender.console.Threshold=DEBUG
log4j.appender.console.layout = org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern={%c}-%m%n
#文件输出的相关设置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/flyfish.log
log4j.appender.file.MaxFileSize=10mb
log4j.appender.file.Threshold=DEBUG
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern={%p}{%d{yy-MM-dd}}{%c}%m%n
#日志输出级别
log4j.logger.org.mybatis=DEBUG
log4j.logger.java.sql=DEBUG
log4j.logger.java.sql.Statement=DEBUG
log4j.logger.java.sql.ResultSet=DEBUG
log4j.logger.java.sql.PreparedStatement=DEBUG
结尾》》》暂时先更新这么多,有时间会持续更新下去。谢谢各位