在pom.xml中的<dependencies>標籤中加入
<!--log4j紀錄檔 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
在resources資料夾下建立log4j.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<param name="Encoding" value="UTF-8"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%-5p %d{MM-dd HH:mm:ss,SSS} %m (%F:%L) \n"/>
</layout>
</appender>
<logger name="java.sql">
<level value="debug"></level>
</logger>
<logger name="org.apache.ibatis">
<level value="info"></level>
</logger>
<root>
<level value="debug"></level>
<appender-ref ref="STDOUT"/>
</root>
</log4j:configuration>
快速獲取SqlSession,不在每次都定義這麼多項
package com.getsqlsession;
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 M_SelSession {
public static SqlSession getSqlSession() {
InputStream is=null;
try {
//載入核心組態檔
is = Resources.getResourceAsStream("mybatis-config.xml");
} catch (IOException ioException) {
ioException.printStackTrace();
}finally {
//工廠模式,獲取SqlSessionFactoryBuilder
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
//獲取sqlSessionFactory
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);
//通過工廠獲取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession(true); //自動提交事務
return sqlSession;
}
}
}
呼叫時
package com.test;
import com.getsqlsession.M_SelSession;
import com.mapper.UserMapper;
import com.pojo.User;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
public class Test {
public static void main(String[] args) {
SqlSession sqlSession= M_SelSession.getSqlSession(); //快速獲取sqlSession
UserMapper userMapper=sqlSession.getMapper(UserMapper.class); //獲取實體類
}
}
jdbc.Driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.pwd=3124
注意將user和pwd的內容改為自己登陸的MySQL使用者名稱和密碼
<properties resource="data.properties"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.Driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
</dataSource>
</environment>
</environments>
(properties?,settings?,typeAliases?,typeHandlers?,objectFactory?,
objectWrapperFactory?,reflectorFactory?,plugins?,environments?,
databaseIdProvider?,mappers?)
<typeAliases>中的<typeAlias type="com.pojo.User" alias="user"></typeAlias>標籤,用於新增單個的類別名,type屬性為該類的全類名,alias屬性為別名名稱,如果不寫alias預設別名為類名
<package>標籤用於將該包下的全部類起預設別名,便於後面在mapper.xml中使用
<!--給類起別名,在mapper.xml中可以對於查詢的resultType直接使用別名進行使用,不需要全類名了-->
<typeAliases>
<typeAlias type="com.pojo.User" alias="user"></typeAlias>
<package name="com.pojo"/>
</typeAliases>
<select id="selectAllUser" resultType="User">
select * from user
</select>
<select id="selectUserById" resultType="user">
select * from user where userid=2
</select>
<mappers>
<!--方式一,resource屬性值為:UserMapper.xml檔案的路徑+檔名-->
<mapper resource="com/mapper/UserMapper.xml"/>
<!--
使用包的方式引入,會將包下的對映檔案都引入
要求:1.UserMapper.xml檔案的各個資料夾名必須和對應介面的各級資料夾一致
2. UserMapper.xml必須和其對應的介面名一致.
-->
<package name="com/mapper"/>
</mappers>