Mybatis 相关配置文件
db.properties
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名?useSSL=false&serverTimezone=UTC
jdbc.username=用户名
jdbc.password=密码
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:跟标签-->
<configuration>
<!-- 1.引入外部properties资源文件-->
<properties resource="db.properties"/>
<!-- 2.批量设置别名-->
<typeAliases>
<package name="com.yellow.bean"/>
</typeAliases>
<!-- 3.数据库连接环境配置 -->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<!--数据库驱动类-->
<property name="driver" value="${jdbc.driverClass}"/>
<!--数据库连接url-->
<property name="url" value="${jdbc.url}"/>
<!--数据库用户名-->
<property name="username" value="${jdbc.username}"/>
<!--数据库密码-->
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<!-- 4.引入映射文件 -->
<mappers>
<!--扫描接口批量引入 【推荐】 name:dao接口所在的包名-->
<package name="com.yellow.dao"/>
</mappers>
</configuration>
log4j.properties
##设置日志记录到控制台的方式
log4j.appender.std=org.apache.log4j.ConsoleAppender
log4j.appender.std.Target=System.err
log4j.appender.std.layout=org.apache.log4j.PatternLayout
log4j.appender.std.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %5p %c{1}:%L - %m%n
##设置日志记录到文件的方式
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=mylog.txt
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
##日志输出的级别,以及配置记录方案
### 日志输出级别:fatal>error>warn>info>debug 实际开发中一般设置日志输出级别为debug 项目上线后一般设置为error
log4j.rootLogger= debug,std,file
### 项目上线之后的日志输出级别设置:log4j.rootLogger= error,file
SqlSessionFactoryUtils.java
SqlSession工具类: 1.获取SqlSession对象; 2.关闭SqlSession对象
package com.itheima.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;
/**
* SqlSession工具类:
* 1.获取SqlSession对象
* 2.关闭SqlSession对象
* 优化:
* 不需要每次获取SqlSession对象的同时获取创建一个SqlSessionFactory对象
* 使用静态代码块优化sqlSessionFactory代码
*/
public class SqlSessionFactoryUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
try {
//1.加载MyBatis核心配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
//2.获取SqlSessionFactory对象
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
System.err.println("加载MyBatis配置文件失败!");
e.printStackTrace();
}
}
/**
* 获取SqlSession对象
* @return
*/
public static SqlSession getSqlSession(){
//3.获取SqlSession对象返回
return sqlSessionFactory.openSession();
}
/**
* 关闭SqlSession对象并提交事务
* @param sqlSession
*/
public static void closeAndCommit(SqlSession sqlSession){
if(sqlSession!=null){
sqlSession.commit();
sqlSession.close();
}
}
/**
* 关闭SqlSession对象并回滚事务
* @param sqlSession
*/
public static void closeAndRollback(SqlSession sqlSession){
if(sqlSession!=null){
sqlSession.rollback();
sqlSession.close();
}
}
}
映射文件格式 xxx.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.yellow.dao.BrandDao"> <!--namespace 为对应的映射DAO包名-->
</mapper>
相关依赖 pom.xml
<?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>com.yellow</groupId>
<artifactId>brandDemo</artifactId>
<version>1.0</version>
<packaging>war</packaging>
<!--设置Maven项目JDK编译版本为1.8-->
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<!--依赖管理-->
<dependencies>
<!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!--servlet-api-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!--jsp-api-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
<scope>provided</scope>
</dependency>
<!--beanUtils-->
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<!--jstl-->
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--mybatis jar-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.6</version>
</dependency>
<!--MySQL驱动jar包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!-- log start -->
<!-- 日志的具体实现 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- 接口 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<!-- log end -->
</dependencies>
<build>
<plugins>
<!--Tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<!--Tomcat插件配置-->
<!--项目首页访问地址:http://localhost:80/-->
<configuration>
<uriEncoding>UTF-8</uriEncoding>
<!--访问端口号-->
<port>80</port>
<!--项目访问路径-->
<path>/</path>
</configuration>
</plugin>
</plugins>
</build>
</project>


被折叠的 条评论
为什么被折叠?



