一.插件
1.PageHelper 分页
2.lombok 注释 实现实体类set get 实例化等
3.ehcache 缓存至硬盘
二.中文文档:地址
mybatis – MyBatis 3 | 简介https://mybatis.org/mybatis-3/zh/index.html
三.一般配置
1.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> <properties resource="db.properties"/> <settings> <setting name="logImpl" value="LOG4J"/> <setting name="cacheEnabled" value="true"/> </settings> <typeAliases> <package name="com.project.pojo"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${dburl}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <package name="com.project.mapper"/> </mappers> </configuration>
2.db.properties
driver=com.mysql.jdbc.Driver dburl=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8 username=root password=123456
3.log4j.properties
#将等级为DEBUG的日志信息输出到console和file这两个目的地,console和file的定义在下面的代码 log4j.rootLogger=DEBUG,console,file #控制台输出的相关设置 log4j.appender.console = org.apache.log4j.ConsoleAppender log4j.appender.console.Target = System.out log4j.appender.console.Threshold=DEBUG log4j.appender.console.encoding=UTF-8 log4j.appender.console.layout = org.apache.log4j.PatternLayout log4j.appender.console.layout.ConversionPattern= [%c]-%m%n #文件输出的相关设置 log4j.appender.file = org.apache.log4j.RollingFileAppender log4j.appender.file.File=./log/log.log log4j.appender.file.MaxFileSize=10mb log4j.appender.file.Threshold=DEBUG log4j.appender.file.encoding=UTF-8 log4j.appender.file.layout=org.apache.log4j.PatternLayout log4j.appender.file.layout.ConversionPattern=[%p] [%d{yy-MM-dd}] [%c] %m%n #日志输出级别 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
四.mybatis工具类
package com.lida.utils;
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;
import java.util.Properties;
public class MybatisUtils {
static SqlSessionFactory sqlSessionFactory ;
static{
String url = "mybatis-config.xml";
try {
InputStream resourceAsStream = Resources.getResourceAsStream(url);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
static public SqlSession getSession(){
return sqlSessionFactory.openSession();
}
}
五.实现查询
1.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">
<mapper namespace="com.lida.dao.UserMapper">
<resultMap id="userMap" type="user">
<result column="pwd" property="password"/>
</resultMap>
<select id="getUserList" resultType="user" >
select * from user ;
</select>
<insert id="addUser" parameterType="user">
insert into user (name,address,pwd) values (#{name},#{address},#{pwd});
</insert>
<update id="updateUser" parameterType="user">
update user set name=#{name},address=#{address},pwd=#{pwd} where id =#{id};
</update>
<delete id="deleteUser" parameterType="int">
delete from user where id=#{id};
</delete>
</mapper>
2.注释
package com.project.mapper;
import com.project.pojo.User;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import java.util.ArrayList;
public interface UserMapper {
@Select("select * from user limit #{start},#{nums}")
ArrayList<User> getUserList(@Param("start") int start, @Param("nums")int nums);
ArrayList<User> getAll();
}