MyBatis環(huán)境資源配置實(shí)現(xiàn)代碼詳解
1. 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>groupId</groupId>
<artifactId>MyBatis_01</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<!--導(dǎo)入相關(guān)依賴(lài)-->
<dependencies>
<!--導(dǎo)入MyBatis相關(guān)jar包-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.2</version>
</dependency>
<!--導(dǎo)入junit測(cè)試jar包-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<!--導(dǎo)入連接mysql數(shù)據(jù)庫(kù)驅(qū)動(dòng)包-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--導(dǎo)入MyBatis日志jar包-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<!-- 解決Maven靜態(tài)資源過(guò)濾問(wèn)題 -->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
<!-- 解決版本問(wèn)題 -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2. MyBatis核心配置文件(如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>
<!--導(dǎo)入properties 配置文件-->
<properties resource="db.properties"/>
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
<environments default="development">
<!--環(huán)境配置默認(rèn)是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>
<!--Mybatis的mapper注冊(cè),定義 SQL 映射語(yǔ)句-->
<mappers>
<mapper resource="com/lf/dao/UserMapper.xml"/>
</mappers>
</configuration>
Properties優(yōu)化 :可在資源目錄下新建一個(gè)db.properties 文件。如:
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=true&useUnicode=true&characterEncoding=UTF-8
username=root
password=123456
typeAliases優(yōu)化 :類(lèi)型別名是為 Java 類(lèi)型設(shè)置一個(gè)短名字,只于xml配置有關(guān)
<typeAliases>
<typeAlias type="com.lf.pojo.User" alias="User"/>
</typeAliases>
<!--可以在使用 com.kuang.pojo.User 的地方用 User 代替-->
使用標(biāo)準(zhǔn)日志
<!--標(biāo)準(zhǔn)日志實(shí)現(xiàn)-->
<settings>
<setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>
使用 Log4j 日志
Log4j配置文件:(如log4j.properties)
#將等級(jí)為DEBUG的日志信息輸出到console和file這兩個(gè)目的地,console和file的定義在下面的代碼
log4j.rootLogger=DEBUG,console,file
#控制臺(tái)輸出的相關(guān)設(shè)置
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
#文件輸出的相關(guān)設(shè)置
log4j.appender.file = org.apache.log4j.RollingFileAppender
log4j.appender.file.File=./log/kuang.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
#日志輸出級(jí)別
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
setting設(shè)置日志實(shí)現(xiàn)
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
在程序中使用Log4j進(jìn)行輸出
3. Mapper.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"> <!--namespace 十分重要,不能寫(xiě)錯(cuò)--> <mapper namespace="com.lf.dao.UserMapper"> <!--下面寫(xiě)增刪改查語(yǔ)句,--> <select id="getUserList" resultType="com.lf.pojo.User"> select * from mybatis.user </mapper>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
詳解spring applicationContext.xml 配置文件
本篇文章主要介紹了詳解spring applicationContext.xml 配置文件 ,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02
JSON.parseObject和JSON.toJSONString實(shí)例詳解
這篇文章主要為大家詳細(xì)介紹了JSON.parseObject和JSON.toJSONString實(shí)例,具有一定的參考價(jià)值,感興趣的朋友可以參考一下2018-06-06
原生Java操作mysql數(shù)據(jù)庫(kù)過(guò)程解析
這篇文章主要介紹了原生Java操作mysql數(shù)據(jù)庫(kù)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11
SpringBoot引入模板引擎實(shí)現(xiàn)視圖解析
這篇文章主要介紹了SpringBoot引入模板引擎實(shí)現(xiàn)視圖解析方法流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2022-10-10
利用Spring MVC+Mybatis實(shí)現(xiàn)Mysql分頁(yè)數(shù)據(jù)查詢(xún)的過(guò)程詳解
這篇文章主要給大家介紹了關(guān)于利用Spring MVC+Mybatis實(shí)現(xiàn)Mysql分頁(yè)數(shù)據(jù)查詢(xún)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-08-08
java實(shí)現(xiàn)頁(yè)面多查詢(xún)條件必選的統(tǒng)一處理思路
這篇文章主要為大家介紹了java實(shí)現(xiàn)頁(yè)面多查詢(xún)條件必選的統(tǒng)一處理思路詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Springboot啟用多個(gè)監(jiān)聽(tīng)端口代碼實(shí)例
這篇文章主要介紹了Springboot啟用多個(gè)監(jiān)聽(tīng)端口代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
Java GUI進(jìn)階之流式布局管理器FlowLayout專(zhuān)項(xiàng)精講
FlowLayout-流式布局管理器,按水平方向依次排列放置組件,排滿(mǎn)一行,換下一行繼續(xù)排列。排列方向(左到右 或 右到左)取決于容器的componentOrientation屬性2022-04-04

