美文网首页
springboot+mybatis(tk和mapper.xml

springboot+mybatis(tk和mapper.xml

作者: cjlynn | 来源:发表于2021-04-26 10:39 被阅读0次

本文主要讲的是tk的bean操作数据库和mapper.xml写sql操作数据库共存。
同一个项目一个area纯tk操作,一个role纯mapper.xml的sql操作,主要是为了对于复杂sql不想在tk的java代码中写一堆sql。
1、pom.xml

<dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>
<dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.0.4</version>
        </dependency>

2、配置

#mybatis
mybatis:
  configuration:
    map-underscore-to-camel-case: true
#  type-aliases-package: com.xxx.xxx.dao
  mapper-locations: classpath:mapper/system/*.xml
#tk
mapper:
  mappers: com.manis.globalwp.common.BaseMapper 
  not-empty: true
  identity: MYSQL  
  enable-method-annotation: true 

3、代码
AreaDao、RoleMapper.java和RoleMapper.xml

AreaDao.java

public interface AreaDao extends BaseMapper<Area> {
    /**
     * 通过id查询区域对象
     * @param id
     * @return Area
     */
    @Select("select * from area where id = #{id}")
    Area selectAreaById(String id);
}

RoleMapper.java

//@Mapper
public interface RoleMapper
{

    /**
     * 通过角色ID查询角色
     *
     * @param roleId 角色ID
     * @return 角色对象信息
     */
    public Role selectRoleById(Long roleId);

}

RoleMapper.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.xxx.xxx.mapper.RoleMapper">

    <resultMap type="com.manis.globalwp.domain.Role" id="SysRoleResult">
        <id     property="roleId"       column="role_id"        />
        <result property="roleName"     column="role_name"      />
        <result property="roleKey"      column="role_key"       />
        <result property="roleSort"     column="role_sort"      />
        <result property="dataScope"    column="data_scope"     />
        <result property="status"       column="status"         />
        <result property="delFlag"      column="del_flag"       />
        <result property="createBy"     column="create_by"      />
        <result property="createTime"   column="create_time"    />
        <result property="updateBy"     column="update_by"      />
        <result property="updateTime"   column="update_time"    />
        <result property="remark"       column="remark"         />
    </resultMap>

    <sql id="selectRoleVo">
        select distinct r.role_id, r.role_name, r.role_key, r.role_sort, r.data_scope,
            r.status, r.del_flag, r.create_time, r.remark
        from sys_role r
    </sql>

    <select id="selectRoleById" parameterType="Long" resultMap="SysRoleResult">
        <include refid="selectRoleVo"/>
        where r.role_id = #{roleId}
    </select>
</mapper>

启动类GlobalApplication

@SpringBootApplication
@EnableTransactionManagement
@EnableScheduling
// 表示通过aop框架暴露该代理对象,AopContext能够访问
@EnableAspectJAutoProxy(exposeProxy = true)
// 指定要扫描的Mapper类的包的路径
@MapperScan({"com.xxx.**.dao","com.xxx.**.mapper"})
@EnableCaching
// 开启线程异步执行
@EnableAsync
public class GlobalApplication {
    public static void main(String[] args) {
        ApplicationContext context = SpringApplication.run(GlobalApplication.class, args);
    }
}

测试类GlobalApplicationTests.java

@RunWith(SpringRunner.class)
@SpringBootTest
public class GlobalApplicationTests {
    @Autowired
    private AreaService areaService;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private AreaDao areaDao;

    @Test
    public void testRole() {
        Role role = roleMapper.selectRoleById(1L);
        System.out.println(role.getRoleName());
    }
    @Test
    public void testArea() {
        Area area = areaDao.selectAreaById("107c0f08fc7ea267");
        System.out.println(area.getName());
    }
}

测试通过,可以选择性的用tk或者mapper接口。

相关文章

网友评论

      本文标题:springboot+mybatis(tk和mapper.xml

      本文链接:https://www.haomeiwen.com/subject/riisrltx.html