步骤

  1. 引入mybatis依赖 ```xml org.mybatis mybatis 3.4.5
mysql mysql-connector-java 5.1.6

junit junit 4.12

  1. 2. 编写实体类
  2. 3. 编写映射文件UserMapper.xml
  3. ```xml
  4. <?xml version="1.0" encoding="UTF-8"?>
  5. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  6. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  7. <mapper namespace="com.lagou.entity.User">
  8. <select id="findAll" resultType="com.lagou.entity.User">
  9. select * from user
  10. </select>
  11. </mapper>
  1. 编写核心配置类sqlMapper.xml

    1. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    3. <configuration>
    4. <!--environments:运行环境-->
    5. <environments default="development">
    6. <environment id="development">
    7. <!--当前事务交由JDBC管理-->
    8. <transactionManager type="JDBC"/>
    9. <!--使用mybatis提供的连接池-->
    10. <dataSource type="POOLED">
    11. <property name="driver" value="com.mysql.jdbc.Driver"/>
    12. <property name="url" value="jdbc:mysql://localhost:3307/layui?characterEncoding=utf-8"/>
    13. <property name="username" value="root"/>
    14. <property name="password" value="root"/>
    15. </dataSource>
    16. </environment>
    17. </environments>
    18. <mappers>
    19. <mapper resource="UserMapper.xml"/>
    20. </mappers>
    21. </configuration>
  2. 测试类

    1. @Test
    2. public void test1() throws IOException {
    3. InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    4. SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
    5. SqlSession sqlSession = build.openSession();
    6. List<User> users = sqlSession.selectList("com.lagou.entity.User.findAll");
    7. System.out.println(users);
    8. sqlSession.close();
    9. }

    传统的dao层开发方式

  3. 编写Dao接口

    1. public interface UserDao {
    2. List<User> findAll() throws IOException;
    3. }
  4. 编写Dao实现类

    1. public class UserDaoImpl implements UserDao{
    2. @Override
    3. public List<User> findAll() throws IOException {
    4. InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    5. SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
    6. SqlSession sqlSession = build.openSession();
    7. List<User> users = sqlSession.selectList("com.lagou.entity.User.findAll");
    8. System.out.println(users);
    9. sqlSession.close();
    10. return users;
    11. }
    12. }
  5. 测试

    1. @Test
    2. public void test2() throws IOException {
    3. UserDaoImpl userDao = new UserDaoImpl();
    4. List<User> all = userDao.findAll();
    5. System.out.println(all);
    6. }

    代理开发方式

    Mapper接口开发方法只需要程序员编写Mapper接口,由Myabtis开发框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。
    Mapper接口需要遵循以下规范:

  6. Mapper.xml文件中的namespace与mapper接口的全限定类名相同

  7. Mapper接口的方法名和Mapper.xml定义的每个statement的id相同
  8. Mapper接口的输入参数类型和mapper.xml定义的每个parameterType相同
  9. Mapper接口的输出参数类型和mapper.xml定义的resultType类型相同
  10. 测试:

    1. @Test
    2. public void test3() throws IOException {
    3. InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
    4. SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
    5. SqlSession sqlSession = build.openSession();
    6. UserDao mapper = sqlSession.getMapper(UserDao.class);
    7. List<User> all = mapper.findAll();
    8. System.out.println(all);
    9. }

    mybatis核心配置文件的参数详解

  11. 核心配置类sqlMapper.xml

    1. <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
    2. "http://mybatis.org/dtd/mybatis-3-config.dtd">
    3. <configuration>
    4. <!--加载外部properties文件,可以更方便管理数据源参数-->
    5. <properties resource="jdbc.properties"/>
    6. <!--给实体类的全限定类名取别名-->
    7. <!--基本类型的别名mybatis已经配置好:int,string,long,double,boolean...-->
    8. <typeAliases>
    9. <!--给单独的实体取别名-->
    10. <typeAlias type="com.lagou.entity.User" alias="user"/>
    11. <!--批量取别名:就是该包下实体本身的类名,别名不区分大小写-->
    12. <package name="com.lagou.entity"/>
    13. </typeAliases>
    14. <!--environments:运行环境-->
    15. <environments default="development">
    16. <environment id="development">
    17. <!--当前事务交由JDBC管理-->
    18. <transactionManager type="JDBC"/>
    19. <!--使用mybatis提供的连接池-->
    20. <dataSource type="POOLED">
    21. <property name="driver" value="${jdbc.driver}"/>
    22. <property name="url" value="${jdbc.url}"/>
    23. <property name="username" value="${jdbc.username}"/>
    24. <property name="password" value="${jdbc.password}"/>
    25. </dataSource>
    26. </environment>
    27. </environments>
    28. <mappers>
    29. <mapper resource="UserMapper.xml"/>
    30. </mappers>
    31. </configuration>
  12. 外部配置文件jdbc.propertice

    1. jdbc.driver=com.mysql.jdbc.Driver
    2. jdbc.url=jdbc:mysql://localhost:3307/layui?characterEncoding=utf-8
    3. jdbc.username=root
    4. jdbc.password=root

    动态sql编写

  13. 多条件查询if

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.lagou.entity.dao.UserDao">
    5. <!--多条件组合查询,演示if-->
    6. <select id="findByCondition" resultType="com.lagou.entity.User" parameterType="com.lagou.entity.User">
    7. select * from user
    8. <where>
    9. <if test="id != null and id != ''">
    10. and id=#{id}
    11. </if>
    12. <if test="name != null and name != ''">
    13. and name = #{name}
    14. </if>
    15. </where>
    16. </select>
    17. </mapper>
  14. 多值查询foreach

    1. <select id="findByIds" resultType="com.lagou.entity.User">
    2. select * from user
    3. <where>
    4. <foreach collection="array" open="id in (" close=")" separator="," item="id">
    5. #{id}
    6. </foreach>
    7. </where>
    8. </select>
  15. 抽取sql

    1. <sql id="selectUser">
    2. select * from user
    3. </sql>
    4. <select id="findAll" resultType="com.lagou.entity.User">
    5. <include refid="selectUser"></include>
    6. </select>

    复杂映射的开发

    一对一查询

  16. user类

    1. public class User {
    2. private Long id;
    3. private String name;
    4. private List<Order> orderList;
    5. }
  17. order类

    1. public class Order {
    2. private Integer orderId;
    3. private String name;
    4. private User user;
    5. }
  18. 查询语句

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    3. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    4. <mapper namespace="com.lagou.entity.dao.OrderDao">
    5. <resultMap id="OrderMap" type="com.lagou.entity.Order">
    6. <result property="orderId" column="order_id"/>
    7. <result property="name" column="name"/>
    8. <!--order类里的封装的user类-->
    9. <association property="user" javaType="com.lagou.entity.User">
    10. <result property="id" column="id"/>
    11. <result property="name" column="name"/>
    12. </association>
    13. </resultMap>
    14. <select id="findOrderAndUser" resultMap="OrderMap">
    15. SELECT * FROM `order` o,`user` u WHERE o.user_id = u.id
    16. </select>
    17. </mapper>

    一对多查询

    ```xml

  1. <a name="Q5lXj"></a>
  2. ### 多对多查询
  3. ```xml
  4. <resultMap id="roleMap" type="com.lagou.entity.User">
  5. <id property="id" column="uid"/>
  6. <result property="name" column="name"/>
  7. <!--user类里面封装的order集合类-->
  8. <collection property="roleList" ofType="com.lagou.entity.Role">
  9. <id property="rid" column="rid"/>
  10. <result property="rname" column="rname"/>
  11. </collection>
  12. </resultMap>
  13. <select id="findAllRole" resultMap="roleMap">
  14. SELECT * FROM `user` u LEFT JOIN `user_role` ur ON u.id = ur.uid LEFT JOIN `role` r ON r.rid = ur.rid
  15. </select>