目录
MyBatis构成
1.导入依赖(包括log4j日志配置)
<dependencies>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.10</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.39</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.18.0</version>
</dependency>
</dependencies>
2.定义实体类
即根据数据库表中的记录进行实体类的构建。
3.定义核心配置文件(Configaration.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>
<properties resource="db.properties">
<property name="driver" value="com.mysql.jdbc.Driver"/>
</properties>
<settings>
<!--实现驼峰-->
<setting name="mapUnderscoreToCamelCase" value="true"/>
<!--标准日志实现-->
<setting name="logImpl" value="LOG4J2"/>
<!--延迟加载-->
<setting name="lazyLoadingEnabled" value="true"/>
<!--二级缓存-->
<setting name="cacheEnabled" value="true"/>
</settings>
<!-- <typeAliases>-->
<!-- <!– <typeAlias type="com.first.entity.Book" alias="book"></typeAlias>–>-->
<!-- <package name="com.second.entity"/>-->
<!-- </typeAliases>-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="jdbc:mysql://localhost:3306/wang"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- <package name="com.first.dao"/>-->
<mapper resource="com/second/UserMapper.xml"/>
<mapper resource="com/second/DeptMapper.xml"/>
</mappers>
</configuration>
4.定义Dao接口(即mapper)
写出对应的需要的方法
5.定义Mapper映射文件,与Dao接口对应
<?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.second.dao.UserDao">
<select id="selectAll" resultMap="UserResultMap">
select id,name,sex,hiredate,t_dept.deptno deptno,t_dept.dname dname from t_user left join t_dept on t_user.deptno = t_dept.deptno
</select>
<resultMap id="UserResultMap" type="com.second.entity.User" autoMapping="true">
<!-- <id column="id" property="id"></id>-->
<!-- <id column="name" property="name"></id>-->
<!-- <id column="sex" property="sex"></id>-->
<!-- <id column="hiredate" property="hireDate"></id>-->
<association property="dept" javaType="com.second.entity.Dept" autoMapping="true">
<!-- <id column="deptno" property="deptNo"></id>-->
<!-- <id column="dname" property="dname"></id>-->
</association>
</resultMap>
<insert id="insert" parameterType="com.second.entity.User" useGeneratedKeys="true" keyProperty="id">
insert into t_user values(null,#{name},#{sex},#{dept.deptNo},#{hireDate})
</insert>
<update id="update" parameterType="com.second.entity.User">
update t_user set name = #{name},sex = #{sex},deptno = #{dept.deptNo},hiredate = #{hireDate} where id = #{id}
</update>
<delete id="delete" parameterType="com.second.entity.User">
delete from t_user where id = #{id}
</delete>
</mapper>
6.使用MyBatis的API操作
public class TestSome {
//session工厂
SqlSession session = null;
@Before
public void init(){
//1.定义核心配置文件
String resource = "Configuration.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
e.printStackTrace();
}
//2.创建session工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//3.获得session(对话),程序与MyBatis
session = sqlSessionFactory.openSession();
}
@Test
public void testDeptSelect(){
//4.获得Dao对象
DeptDao dao = session.getMapper(DeptDao.class);
List<Dept> list = dao.selectAll();
System.out.print(list);
}
}
7.log4j2.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<configuration status="DEBUG">
<appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</appenders>
<loggers>
<logger name="log4j.logger.noModule" level="fatal" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.org.mybatis.jpetstore" level="trace" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.com.opensymphony.xwork2" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.com.ibatis" level="trace" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.com.ibatis.common.jdbc.SimpleDataSource" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.com.ibatis.common.jdbc.ScriptRunner" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.org.mybatis" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.java.sql" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.java.sql.Connection" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.java.sql.Statement" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.java.sql.PreparedStatement" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<logger name="log4j.logger.java.sql.ResultSet" level="debug" additivity="false">
<appender-ref ref="Console"/>
</logger>
<root level="trace">
<appender-ref ref="Console"/>
</root>
</loggers>
</configuration>