1.基础
1.1 pom
<dependencies><!--MyBatis核心--><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.5.7</version></dependency><!--MySql驱动--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.49</version></dependency></dependencies>
1.2 jdbc.properties
jdbc.driver= com.mysql.jdbc.Driver
jdbc.url= jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8
jdbc.username= root
jdbc.password= root
1.3 核心配置
mybatis-config文件中的标签必须按照固定的顺序(有的标签可以不写,但顺序一定不能乱): properties、settings、typeAliases、typeHandlers、objectFactory、objectWrapperFactory、reflectorFactory、plugins、environments、databaseIdProvider、mappers
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><properties resource="jdbc.properties"/><typeAliases><package name="com.example.mybatis.pojo"/></typeAliases><!--配置连接数据库的环境--><environments default="development"><environment id="development"><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="mappers/PaymentMapper.xml"></mapper></mappers>
</configuration>
1.4 mapper接口
public interface PaymentMapper {List<Payment> selectAll();}
1.5 映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mybatis.mapper"><select id="selectAll" resultMap="Payment">select * from payment</select></mapper>
1.6 文件结构
1.7 测试功能
//读取MyBatis的核心配置文件
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");//获取SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();//通过核心配置文件所对应的字节输入流创建工厂类SqlSessionFactory,生产SqlSession对象
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(is);//获取sqlSession,此时通过SqlSession对象所操作的sql都必须手动提交或回滚事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);//通过代理模式创建UserMapper接口的代理实现类对象
PaymentMapper mapper = sqlSession.getMapper(PaymentMapper.class);List<Payment> payments = mapper.selectAll();
1.8 MyBatisX
安装之后会出现小鸟图标,直接导航和快速生成映射文件
1.9 log4j日志功能
1) pom
<!-- log4j日志 -->
<dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version>
</dependency>
2) log4j.xml文件,存放的位置是src/main/resources目录下
<?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" /></logger><logger name="org.apache.ibatis"><level value="info" /></logger><root> <level value="debug" /><appender-ref ref="STDOUT" /></root>
</log4j:configuration>
2.查询
2.1 无参数
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mybatis.mappers.PaymentMapper"><resultMap id="paymentResultMap" type="Payment"><result column="serial_name" property="serialName"></result></resultMap><select id="selectAll" resultMap="paymentResultMap">select * from payment</select></mapper>
2.2 带参数
1) 接口
public interface PaymentMapper {List<Payment> selectAll(@Param("id") int id);}
2) 映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mybatis.mappers.PaymentMapper"><resultMap id="paymentResultMap" type="Payment"><result column="serial_name" property="serialName"></result></resultMap><select id="selectAll" resultMap="paymentResultMap" >select * from payment where id = #{id}</select></mapper>
2.3 实体参数
1) 接口
public interface PaymentMapper {List<Payment> selectAll(Payment id);
}
2) 映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mybatis.mappers.PaymentMapper"><resultMap id="paymentResultMap" type="Payment"><result column="serial_name" property="serialName"></result></resultMap><select id="selectAll" resultMap="paymentResultMap" >select *from paymentwhere id = #{id}and serial_name like concat('%',#{serialName,jdbcType=VARCHAR},'%')and serial_name like "%"#{serialName,jdbcType=VARCHAR}"%"</select></mapper>
2.4 map参数
1) 接口
public interface PaymentMapper {List<Payment> selectAll(Map<String,Object> map);
}
2) 映射文件
用2.3即可
2.5 动态条件 if
1) 接口
按2.3即可
2) 映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mybatis.mappers.PaymentMapper"><resultMap id="paymentResultMap" type="Payment"><result column="serial_name" property="serialName"></result></resultMap><select id="selectAll" resultMap="paymentResultMap" >select *from payment<where><if test="serialName != null and serialName != ''">and serial_name like "%"#{serialName,jdbcType=VARCHAR}"%"</if></where></select>
</mapper>
2.6 动态条件 choose
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.example.mybatis.mappers.PaymentMapper"><resultMap id="paymentResultMap" type="Payment"><result column="serial_name" property="serialName"></result></resultMap><select id="selectAll" resultMap="paymentResultMap" >select *from paymentwhere<choose><when test="serialName != null and serialName != ''">serial_name like "%"#{serialName,jdbcType=VARCHAR}"%"</when><otherwise>1=1</otherwise></choose></select>
</mapper>
3.插入
3.1 映射文件
<insert id="insert" parameterType="payment"useGeneratedKeys="true"keyProperty="id">insert into payment(serial_name)values (#{serialName})
</insert>
3.2