bkt项目 (三)搭建环境并测试2之MyBatis映射方式更改

简介:这个项目是为了学习SpringBoot以及学习SpringCloud用的,如果对你有什么帮助,还是非常高兴的。

GitHub :   https://github.com/fankf/bkt.git

技术使用 :SpringBoot + SSM + MySql 

IDE :  STS

之前采用的是注解方式做测试,但是考虑到可能采用比较复杂的sql语句,所以更改成XML文件作为映射方式。

更改配置:

# mybatis mapper  配置 此处添加 mapper 文件映射地址包名com.example.dao.xml,文件名 *.xml
mybatis:
  type-aliases-package: com.example.dao
  mapper-locations: classpath:com/example/dao/xml/*.xml

Dao更改:(注销注解)

public interface TestDao {
/*    @Select("select * from test")
    @Results({
        @Result(property="id",column="id"),
        @Result(property="testName",column="name")
    })*/
    List<Test> getTestList();
    
}

映射XML文件TestDao.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.example.dao.TestDao">
    <sql id="columns">
        id AS "id",
        test_name AS "testName"
    </sql>
    <select id="getTestList" resultType="com.example.entity.Test">
        select 
            <include refid="columns"/>
        from test
    </select>
</mapper>

请求:

http://localhost:8080/test

结果 :

[{"id":1,"testName":"测试1"},{"id":2,"testName":"测试2"}]

 

posted @ 2018-08-23 16:15  FN飞鸟  阅读(144)  评论(0编辑  收藏  举报