画布 1
Mybatis
  开启debug日志
    1.pom添加坐标log4j
    2.sqlmapConfig.xml配置文件
      <settings >
<setting name="logImpl" value="LOG4J" />
<setting name="autoMappingBehavior" value="FULL"/>
</settings>
    3.resource/log4j.properties
      ### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.err
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### direct messages to file mylog.log ###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=/tmp/mylog.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n
### set log levels - for more verbose logging change 'info' to 'debug' ###
log4j.rootLogger=debug,stdout
  入门步骤
    1.添加pom坐标
      mysql-connect-java/mybatis
    2.创建实体映射类
    3.添加sqlmapconfig.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"/>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="it/webapp/dao/UserinfoMapper.xml"/>
</mappers>
</configuration>
    4.创建sql语句类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="Userinfo">
<select id="findall" resultType="it.webapp.domain.Userinfo">
select * from Userinfo where 1
</select>
<select id="findOne" resultType="it.webapp.domain.Userinfo" parameterType="int">
select * from Userinfo where id = #{id}
</select>
</mapper>
    5.测试
      public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapconfig.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sessionFactory.openSession();
List<Userinfo> users = sqlSession.selectList("Userinfo.findall");
Userinfo user_ = sqlSession.selectOne("Userinfo.findOne", 1);
System.out.println(user_);
System.out.println(users);
}
  动态代理映射
(无需创建Dao层实现代码)
    1.添加pom坐标
      mysql-connect-java/mybatis
    2.创建实体映射类
    3.创建Dao层接口
      public interface userDao {
public List<Userinfo> getUsers();
}
    4.创建sqlmapconfig.xml配置文件
      同入门配置
    5.创建sql语句类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="it.webapp.dao.userDao">
<select id="getUsers" resultType="it.webapp.domain.Userinfo">
select * from Userinfo where 1
</select>
</mapper>
    6.测试
      public static void main(String[] args) throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapconfig.xml");
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = sessionFactory.openSession();
userDao mapper = sqlSession.getMapper(userDao.class); //关键点，只需要写Dao层接口类,可获得dao层实例
List<Userinfo> users = mapper.getUsers();
System.out.println(users);
}
    注意点：
1.接口的全路径和namespace一致
2.接口的方法名和id一致
3.接口的返回值和resultType一致
  动态SQL
    if子句
      <select id="getUserinfoById" resultType="it.webapp.domain.Userinfo" parameterType="it.webapp.domain.Userinfo">
select * from Userinfo
<where>
<if test="id!=0">
id = #{id}
</if>
<if test="username!=null">
and username = #{username}
</if>
</where>
</select>
    foreach子句
      <select id="getUsersByIds" parameterType="list" resultType="it.webapp.domain.Userinfo">
select * from Userinfo
<where>
<foreach collection="list" open="id in (" close=")" item="id" separator=",">
#{id}
</foreach>
</where>
</select>
        select ..... WHERE id in ( ? , ? , ? )
      如果传入的接口的参数单参数且参数类型是一个List的时候，collection属性值为list .
如果传入的接口的参数是单参数且参数类型是一个array数组的时候，collection的属性值为array .
如果传入的接口的参数是对象,对象里面封装了list或者array的话，collection的值为对象的list的值
  插件使用
    分页插件的使用
      1.pom引入依赖
        <dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.0.0</version>
</dependency>
      2.sqlmapper.xml添加插件配置
        <plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql"/>
</plugin>
</plugins>
      测试
  多表查询
    1对1
      备注：无法使用resultType,而是使用resultMap
      property：实体类的属性名
      column：数据库查询出来的字段
      type：数据转换为实体类的全路径类名
    1对多
      备注：无法使用resultType,而是使用resultMap
      property：实体类的属性名
      column：数据库查询出来的字段
      type：数据转换为实体类的全路径类名
      ofType: 属性名中的类对应的实体类
    多对多
      同1对多，一般会引入中间表产生关联