<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/' >
<appender name="myConsole" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" />
layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="levelMin" value="debug" />
<param name="levelMax" value="warn" />
<param name="AcceptOnMatch" value="true" />
filter>
appender>
<appender name="myFile" class="org.apache.log4j.RollingFileAppender">
<param name="File" value="E:/output.log" />
<param name="Append" value="true" />
<param name="MaxBackupIndex" value="10" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p (%c:%L)- %m%n" />
layout>
appender>
<appender name="activexAppender" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="E:/activex.log" />
<param name="DatePattern" value="'.'yyyy-MM-dd'.log'" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" />
layout>
appender>
<logger name="com.runway.bssp.activeXdemo" additivity="false">
<appender-ref ref="activexAppender" />
logger>
<root>
<appender-ref ref="myConsole"/>
<appender-ref ref="myFile"/>
root>
log4j:configuration>
2.创建Mybatis的核心配置文件(取名为Mybatis-config.xml),添加jdbc.properties数据库连接参数配置文件
<configuration>
<properties resource="jdbc.properties">properties>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
settings>
<typeAliases>
<typeAlias type="com.ourenlongblog.bean.User" alias="User"/>
typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC">transactionManager>
<dataSource type="POOLED">
<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>
<mappers>
<mapper resource="UserMapper.xml" />
mappers>
configuration>
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ourenlongbk?characterEncoding=UTF-8&characterSetResults=UTF-8&zeroDateTimeBehavior=convertToNull&serverTimezone=UTC
jdbc.username=root
jdbc.password=root
3.创建映射文件,并配置
例如实体类:
package com.ourenlongblog.bean;
public class User {
private Integer id;
private String userName;
private String password;
private Integer age;
public User() {
}
public User(Integer id, String userName, String password, Integer age) {
super();
this.id = id;
this.userName = userName;
this.password = password;
this.age = age;
}
@Override
public String toString() {
return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", age=" + age + "]";
}
}
映射配置文件UserMapper.xml:
<mapper namespace="com.ourenlongblog.Mapper.UserMapper">
<select id="getUserById" resultType="com.ourenlongblog.bean.User" >
select * from user where id = #{id}
select>
mapper>
4.创建Mapper接口,实现两个绑定
(1)创建UserMapper接口
package com.ourenlongblog.Mapper;
import com.ourenlongblog.bean.User;
public interface UserMapper {
User getUserById(String id);
}
(2)绑定UserMapper.xml中的:
<mapper namespace="com.ourenlongblog.Mapper.UserMapper">
(3) 绑定id=接口中的方法名,resultType=SQL语句执行后返回的数据类型
<select id="getUserById" resultType="com.ourenlongblog.bean.User" >
5.获取Mybatis操作数据会话对象SqlSession
InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession();
6.测试
package com.ourenlongblog.Mapper;
import java.io.IOException;
import java.io.InputStream;
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 org.junit.jupiter.api.Test;
import com.ourenlongblog.bean.User;
class TestMybatis {
@Test
void test() throws IOException {
InputStream is=Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sessionFactory.openSession();
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.getUserById("1");
System.out.println(user);
}
}