步骤
- 引入mybatis依赖
```xml
org.mybatis mybatis 3.4.5
2. 编写实体类
3. 编写映射文件UserMapper.xml
```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.lagou.entity.User">
<select id="findAll" resultType="com.lagou.entity.User">
select * from user
</select>
</mapper>
编写核心配置类sqlMapper.xml
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--environments:运行环境-->
<environments default="development">
<environment id="development">
<!--当前事务交由JDBC管理-->
<transactionManager type="JDBC"/>
<!--使用mybatis提供的连接池-->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3307/layui?characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="UserMapper.xml"/>
</mappers>
</configuration>
测试类
@Test
public void test1() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
List<User> users = sqlSession.selectList("com.lagou.entity.User.findAll");
System.out.println(users);
sqlSession.close();
}
传统的dao层开发方式
编写Dao接口
public interface UserDao {
List<User> findAll() throws IOException;
}
编写Dao实现类
public class UserDaoImpl implements UserDao{
@Override
public List<User> findAll() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
List<User> users = sqlSession.selectList("com.lagou.entity.User.findAll");
System.out.println(users);
sqlSession.close();
return users;
}
}
测试
@Test
public void test2() throws IOException {
UserDaoImpl userDao = new UserDaoImpl();
List<User> all = userDao.findAll();
System.out.println(all);
}
代理开发方式
Mapper接口开发方法只需要程序员编写Mapper接口,由Myabtis开发框架根据接口定义创建接口的动态代理对象,代理对象的方法体同上边Dao接口实现类方法。
Mapper接口需要遵循以下规范:Mapper.xml文件中的namespace与mapper接口的全限定类名相同
- Mapper接口的方法名和Mapper.xml定义的每个statement的id相同
- Mapper接口的输入参数类型和mapper.xml定义的每个parameterType相同
- Mapper接口的输出参数类型和mapper.xml定义的resultType类型相同
测试:
@Test
public void test3() throws IOException {
InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
SqlSessionFactory build = new SqlSessionFactoryBuilder().build(resourceAsStream);
SqlSession sqlSession = build.openSession();
UserDao mapper = sqlSession.getMapper(UserDao.class);
List<User> all = mapper.findAll();
System.out.println(all);
}
mybatis核心配置文件的参数详解
核心配置类sqlMapper.xml
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--加载外部properties文件,可以更方便管理数据源参数-->
<properties resource="jdbc.properties"/>
<!--给实体类的全限定类名取别名-->
<!--基本类型的别名mybatis已经配置好:int,string,long,double,boolean...-->
<typeAliases>
<!--给单独的实体取别名-->
<typeAlias type="com.lagou.entity.User" alias="user"/>
<!--批量取别名:就是该包下实体本身的类名,别名不区分大小写-->
<package name="com.lagou.entity"/>
</typeAliases>
<!--environments:运行环境-->
<environments default="development">
<environment id="development">
<!--当前事务交由JDBC管理-->
<transactionManager type="JDBC"/>
<!--使用mybatis提供的连接池-->
<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="UserMapper.xml"/>
</mappers>
</configuration>
外部配置文件jdbc.propertice
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3307/layui?characterEncoding=utf-8
jdbc.username=root
jdbc.password=root
动态sql编写
多条件查询if
<?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.lagou.entity.dao.UserDao">
<!--多条件组合查询,演示if-->
<select id="findByCondition" resultType="com.lagou.entity.User" parameterType="com.lagou.entity.User">
select * from user
<where>
<if test="id != null and id != ''">
and id=#{id}
</if>
<if test="name != null and name != ''">
and name = #{name}
</if>
</where>
</select>
</mapper>
多值查询foreach
<select id="findByIds" resultType="com.lagou.entity.User">
select * from user
<where>
<foreach collection="array" open="id in (" close=")" separator="," item="id">
#{id}
</foreach>
</where>
</select>
抽取sql
<sql id="selectUser">
select * from user
</sql>
<select id="findAll" resultType="com.lagou.entity.User">
<include refid="selectUser"></include>
</select>
复杂映射的开发
一对一查询
user类
public class User {
private Long id;
private String name;
private List<Order> orderList;
}
order类
public class Order {
private Integer orderId;
private String name;
private User user;
}
查询语句
<?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.lagou.entity.dao.OrderDao">
<resultMap id="OrderMap" type="com.lagou.entity.Order">
<result property="orderId" column="order_id"/>
<result property="name" column="name"/>
<!--order类里的封装的user类-->
<association property="user" javaType="com.lagou.entity.User">
<result property="id" column="id"/>
<result property="name" column="name"/>
</association>
</resultMap>
<select id="findOrderAndUser" resultMap="OrderMap">
SELECT * FROM `order` o,`user` u WHERE o.user_id = u.id
</select>
</mapper>
一对多查询
```xml
<a name="Q5lXj"></a>
### 多对多查询
```xml
<resultMap id="roleMap" type="com.lagou.entity.User">
<id property="id" column="uid"/>
<result property="name" column="name"/>
<!--user类里面封装的order集合类-->
<collection property="roleList" ofType="com.lagou.entity.Role">
<id property="rid" column="rid"/>
<result property="rname" column="rname"/>
</collection>
</resultMap>
<select id="findAllRole" resultMap="roleMap">
SELECT * FROM `user` u LEFT JOIN `user_role` ur ON u.id = ur.uid LEFT JOIN `role` r ON r.rid = ur.rid
</select>