问题的引入
这几天在学maven多模块,练习了下。最初那个maven项目我是用的jdbc来访问数据的,只用在dao层就可以独立的实现,然后service层就可以调用了。
但是,近期这个springmvc项目我想用druid+mybatis来操作数据库,相对于用jdbc的话,这就比较麻烦了
用mybatis框架如何写dao层模块呢
首先要知道,用mybatis访问数据库和jdbc是不一样的,jdbc可以独立的访问数据库,但是mybatis框架有很多的配置,而且是依赖于web的,所以mybatis作为框架的dao层需要web层来启动。
解决办法
dao层模块:
没错,dao层放的是mapper和xml文件,其他的所有配置文件都放在了web层,因为它需要web层启动管理。下面简单展示下mapper和xml代码
UserMapper:
package org.root.ambow.dao;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.ibatis.annotations.Param;
import org.root.ambow.model.User;
import org.root.ambow.model.vo.UserVo;
import java.util.List;
import java.util.Map;
/**
*
* User 表数据库控制层接口
*
*/
public interface UserMapper extends BaseMapper<User> {
//验证登录
UserVo selectUserByLoginName(@Param("loginName")String username);
UserVo selectUserVoById(@Param("id") Long id);
List<Map<String, Object>> selectUserPage(Pagination page, Map<String, Object> params);
}
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="org.root.ambow.dao.UserMapper">
<resultMap id="userVoResultMap" type="org.root.ambow.model.vo.UserVo">
<id column="id" property="id" jdbcType="BIGINT"/>
<result column="login_name" property="loginName" jdbcType="VARCHAR"/>
<result column="name" property="name" jdbcType="VARCHAR"/>
<result column="password" property="password" jdbcType="VARCHAR"/>
<result column="sex" property="sex" jdbcType="TINYINT"/>
<result column="age" property="age" jdbcType="TINYINT"/>
<result column="user_type" property="userType" jdbcType="TINYINT"/>
<result column="status" property="status" jdbcType="TINYINT"/>
<result column="organization_id" property="organizationId" jdbcType="INTEGER"/>
<result column="create_time" property="createTime" jdbcType="TIMESTAMP"/>
<result column="phone" property="phone" jdbcType="VARCHAR"/>
<result column="organizationName" property="organizationName" jdbcType="VARCHAR"/>
<collection property="rolesList" ofType="Role">
<id column="roleId" property="id"/>
<result column="roleName" property="name"/>
</collection>
</resultMap>
<!-- 通用查询结果列-->
<sql id="Base_Column_List">
id, login_name , name, password, salt, sex, age, phone, user_type , status, organization_id , create_time
</sql>
<!--验证用户登录-->
<select id="selectUserByLoginName" resultMap="userVoResultMap">
SELECT
<include refid="Base_Column_List"/>
from user
where login_name=#{loginName}
</select>
<select id="selectUserVoById" resultMap="userVoResultMap" parameterType="java.lang.Long">
SELECT
t.id,
login_name,
t.`name`,
`password`,
sex,
age,
user_type,
t.`status`,
organization_id,
t.create_time,
phone,
s.`name` AS organizationName,
o.id AS roleId,
o.`name` AS roleName
FROM
user t
LEFT JOIN user_role r ON t.id = r.user_id
LEFT JOIN role o ON r.role_id = o.id
LEFT JOIN organization s ON s.id = t.organization_id
<where>
t.id = #{id}
</where>
</select>
<select id="selectUserPage" resultType="Map">
SELECT
t.id, t.login_name AS loginName, t.`name`, t.phone, t.`password`,
t.sex, t.age, t.create_time AS createTime, t.user_type AS userType,
t.`status`, t.organization_id AS organizationId,
s.`name` AS organizationName, group_concat(o.`name`) AS rolesList
FROM user t
LEFT JOIN user_role r ON t.id = r.user_id
LEFT JOIN role o ON r.role_id = o.id
LEFT JOIN organization s ON s.id = t.organization_id
<where>
<if test=" name != null and name != '' ">
t.name = #{name}
</if>
<if test=" organizationId != null ">
t.organization_id = #{organizationId}
</if>
<if test=" startTime != null ">
<![CDATA[ and t.create_time >= #{startTime} ]]>
</if>
<if test=" endTime != null ">
<![CDATA[ and t.create_time <= #{endTime} ]]>
</if>
</where>
GROUP BY t.id
</select>
</mapper>
web层模块:
值得注意的是:如果不分maven模块的话,mybatis的配置文件里面肯定是要对mybatis的XML文件进行扫描的。同理,多模块情况下的web层模块也需要对XML文件进行扫描,关键代码如下:
<!-- Spring整合Mybatis -->
<bean id="sqlSessionFactory" class="com.baomidou.mybatisplus.spring.MybatisSqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!-- 自动扫描Mapping.xml文件 ,注意classpath:指定的是resource文件目录-->
<property name="mapperLocations" value="classpath*:/sqlMapperXml/*.xml"></property>
<property name="configLocation" value="classpath:xml/mybatis-config.xml"></property>
<!--model里面装的是实体类-->
<property name="typeAliasesPackage" value="org.root.ambow.model"/>
<property name="globalConfig" ref="globalConfig"/>
<property name="plugins">
<array>
<!-- 分页插件配置 -->
<bean id="paginationInterceptor" class="com.baomidou.mybatisplus.plugins.PaginationInterceptor">
<property name="dialectType" value="mysql"/>
<property name="optimizeType" value="aliDruid" />
</bean>
</array>
</property>
</bean>
特别注意的是:
<property name="mapperLocations" value="classpath*:/sqlMapperXml/*.xml"></property>
mapperLocations是指向xml文件的,web层访问dao层的xml文件一定要在classpath后面加上“ * ”才能扫描到。
那么如何进行测试呢
参考:
package org.root.ambow.web;
/**
* @author: 冉庆
* @date: 2019/8/7 10:56
* @function:
* @version: 1.0$
* @description:
*/
import org.junit.Test;
import org.junit.runner.RunWith;
import org.root.ambow.dao.UserMapper;
import org.root.ambow.model.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
/**
* 通用测试类
* @author L.cm
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
"classpath:spring-mvc.xml",
"classpath:spring-config.xml"
})
@WebAppConfiguration
public class BaseTest {
@Autowired
UserMapper userMapper;
// @Autowired
// UserService userService;
@Test
public void test(){
UserVo uservo=userMapper.selectUserVoById(13L);
System.out.println(uservo.toString());
}
}