多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
**1. 创建一个Maven项目** **2. 在`pom.xml`中引入 mybatis 依赖** ```xml <dependencies> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.9</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.49</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.16</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.22</version> </dependency> </dependencies> ``` **3. 创建配置文件`resources/jdbc.properties`** ```properties #########数据库配置信息######### jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/learn_mybatis?useUnicode=true&characterEncoding=UTF-8 jdbc.username=root jdbc.password=root ``` **4. 创建配置文件`resources/log4j.properties`** ``` ############## log4j 日志相关配置 ############## log4j.rootLogger=DEBUG,CONSOLE,file #log4j.rootLogger=ERROR,ROLLING_FILE log4j.logger.cn.smbms.dao=debug log4j.logger.com.ibatis=debug log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug log4j.logger.java.sql.Connection=debug log4j.logger.java.sql.Statement=debug log4j.logger.java.sql.PreparedStatement=debug log4j.logger.java.sql.ResultSet=debug log4j.logger.org.tuckey.web.filters.urlrewrite.UrlRewriteFilter=debug ###################################################################################### # Console Appender 日志在控制输出配置 ###################################################################################### log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender log4j.appender.Threshold=error log4j.appender.CONSOLE.Target=System.out log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLE.layout.ConversionPattern= [%p] %d %c - %m%n ###################################################################################### # DailyRolling File 每天产生一个日志文件,文件名格式:log2009-09-11 ###################################################################################### log4j.appender.file=org.apache.log4j.DailyRollingFileAppender log4j.appender.file.DatePattern=yyyy-MM-dd log4j.appender.file.File=log.log log4j.appender.file.Append=true log4j.appender.file.Threshold=error log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=%d{yyyy-M-d HH:mm:ss}%x[%5p](%F:%L) %m%n log4j.logger.com.opensymphony.xwork2=error ``` **5. 创建 mybatis 核心配置文件`resources/mybatis-config.xml`** ```xml <?xml version="1.0" encoding="UTF-8" ?> <!-- mybatis文档说明 --> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 1. 引入配置文件 jdbc.properties --> <properties resource="jdbc.properties"/> <!-- 2. 设置自动映射,自动映射的作用请查看官方文档: https://mybatis.org/mybatis-3/zh/configuration.html#settings --> <settings> <setting name="autoMappingBehavior" value="FULL"/> </settings> <!-- 3. 给每个实体类起一个别名 (1)规则为:实体类为User,则别名为user (2)即使起了别名,User还是可以正常使用 (3)learn.mybatis01.pojo为实体类所在的包名 --> <typeAliases> <package name="learn.mybatis01.pojo"/> </typeAliases> <!-- 4. 数据源环境配置,其具体含义参考官网:https://mybatis.org/mybatis-3/zh/configuration.html#settings --> <environments default="development"> <environment id="development"> <!-- 事务管理器,分别有JDBC和manage管理器 --> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <!-- 引用 jdbc.properties 中的配置 --> <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> <!-- 5. 配置mapper,否则无法映射到dao层。下面将会创建 resources/mapper/StudentMapper.xml 文件--> <mappers> <mapper resource="mapper/StudentMapper.xml"/> </mappers> </configuration> ``` **6. 环境搭建完毕!**