springboot从零开始[2] gradle+mybatis xml

来源:互联网 发布:小学考试软件下载 编辑:程序博客网 时间:2024/06/11 20:44

springboot从零开始[2] gradle+mybatis xml

[toc]
昨天,我用了注解的方式实现了整合;今天,又花了些时间,把xml的整了出来。

先来看看整体结构
这里写图片描述

1. 准备工作,建表插值。

2. 配置文件的更改

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/castlespring.datasource.username=rootspring.datasource.password=1234spring.datasource.driverClassName=com.mysql.jdbc.Drivermybatis.config-location=classpath:mybatis-config.xmllogging.level.root=INFOlogging.level.top.aircastle.springboot.springboot_xml.dao=DEBUG#mybatis配置#Mapper.xml所在的位置mybatis.mapper-locations=classpath*:top/aircastle/**/dao/*Dao.xml#entity扫描的包名mybatis.type-aliases-package=top.aircastle.springboot.springboot_xml.domain 

这里需要注意的是多了下面的mybatis配置,它的作用就是扫描指定的xml和实体类(老实说,真没明白为什么需要指定实体类),就不需要一个个的去死配了。

mybatis.config-location没变

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"        "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration>   <settings>        <!-- 使用驼峰命名法转换字段。 -->        <setting name="mapUnderscoreToCamelCase" value="true"/>    </settings></configuration>

build.gradle添加了后面的几个任务,目的是从dao包中copy出xml文件。我所知道的有两种:1.新建copy任务;2.改写gradle的processResources方法。这里用的是第一个。

group 'top.aircastle.springboot'version '1.0-SNAPSHOT'apply plugin: 'war'apply plugin: 'eclipse'apply plugin: 'idea'sourceCompatibility = 1.8targetCompatibility = 1.8[compileJava, compileTestJava, javadoc]*.options*.encoding = 'UTF-8'buildscript {    ext {        springBootVersion = '1.5.5.RELEASE'    }    repositories {        mavenLocal()    }    dependencies {        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")    }}war {    baseName = 'springboot-xml'    version = '0.0.1-SNAPSHOT'}repositories {    mavenLocal()    mavenCentral()}dependencies {    compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.38'    compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.5.RELEASE'    compile group: 'org.mybatis.spring.boot', name: 'mybatis-spring-boot-starter', version: '1.2.1'    compile 'org.apache.commons:commons-lang3:3.1'}task wrapper(type: Wrapper) {    gradleVersion = '4.0'}task cleanSqlmaps(type: Delete) {    def files = fileTree("${buildDir}/classes/java", {        include "**/*.xml"    })    delete files}task copySqlmaps(type: Copy, dependsOn: cleanSqlmaps) {    from "${projectDir}/src/main/java"    include "**/*.xml"    into "${buildDir}/classes/java/main"    includeEmptyDirs = false}processResources.dependsOn {    copySqlmaps}

所以你看到build文件夹中是介样的,个人是很中意这样的写法,xml就在dao旁边,多省事儿啊!
这里写图片描述

3. 代码

实体类User是不会改的

package top.aircastle.springboot.springboot_xml.domain;/** * Created by Castle on 2017/8/9. */public class User {    private String id;    private String userName;    private String password;    private String createDate;    private String createBy;    public String getId() {        return id;    }    public void setId(String id) {        this.id = id;    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password;    }    public String getCreateDate() {        return createDate;    }    public void setCreateDate(String createDate) {        this.createDate = createDate;    }    public String getCreateBy() {        return createBy;    }    public void setCreateBy(String createBy) {        this.createBy = createBy;    }}

这边我列出了两种Mapper绑定方式,很显然第一种会更喜人

1.接口方式实现
UserDao.java
特别注意命名必须一致!@Mapper注解不能掉!我之前的spring项目中是不需要注解的,但这个或许是因为springboot的问题必须加上。其中原因之后有机会在研究研究

package top.aircastle.springboot.springboot_xml.dao;import org.apache.ibatis.annotations.Mapper;import top.aircastle.springboot.springboot_xml.domain.User;import java.util.List;import java.util.Map;/** * Created by Castle on 2017/8/5. */@Mapperpublic interface UserDao {    List<User> selectUser(Map<String, String> parm);}

UserDao.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="top.aircastle.springboot.springboot_xml.dao.UserDao">    <select id="selectUser" parameterType="java.util.Map" resultType="top.aircastle.springboot.springboot_xml.domain.User">        select * from user        <where>            <if test="userName!=null and userName!=''">               user_name = #{userName}            </if>        </where>    </select></mapper>

2.mapperId绑定
UsersDao.java

package top.aircastle.springboot.springboot_xml.dao;import org.apache.ibatis.session.SqlSession;import org.springframework.stereotype.Component;import top.aircastle.springboot.springboot_xml.domain.User;/** * Created by Castle on 2017/8/5. */@Componentpublic class UsersDao {    private final SqlSession sqlSession;    public UsersDao(SqlSession sqlSession) {        this.sqlSession = sqlSession;    }    public User selectUsersByName(String userName) {        return this.sqlSession.selectOne("selectUsersByName", userName);    }}

UsersDao.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="top.aircastle.springboot.springboot_xml.dao.UsersDao">    <select id="selectUsersByName" parameterType="java.lang.String"            resultType="top.aircastle.springboot.springboot_xml.domain.User">        select * from user where user_name = #{userName}    </select></mapper>

控制类UserController.java

package top.aircastle.springboot.springboot_xml.controller;import org.apache.commons.lang3.StringUtils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import top.aircastle.springboot.springboot_xml.dao.UserDao;import top.aircastle.springboot.springboot_xml.dao.UsersDao;import top.aircastle.springboot.springboot_xml.domain.User;import java.util.HashMap;import java.util.List;import java.util.Map;/** * Created by Castle on 2017/8/5. */@RestController@RequestMapping("/xml/user")public class UserController {    @Autowired    UsersDao usersDao;    @Autowired    UserDao userDao;    @RequestMapping(value = "/getUser", method = RequestMethod.POST)    public Map getUser(@RequestParam(name = "userName", required = false) String userName) {        Map<String, Object> map = new HashMap();        map.put("status", "SUCCESS");        User user = null;        if (StringUtils.isNotBlank(userName)) {            user = usersDao.selectUsersByName(userName);        }        map.put("data", user);        return map;    }    @RequestMapping(value = "/selectUser", method = RequestMethod.POST)    public Map selectUser(@RequestParam(name = "userName", required = false) String userName) {        Map<String, Object> map = new HashMap();        map.put("status", "SUCCESS");        Map<String, String> parm = new HashMap<>();        if (StringUtils.isNotBlank(userName)){        parm.put("userName", userName);}        List<User> data = userDao.selectUser(parm);        map.put("data", data);        return map;    }}

如此,gradle+mybatis xml就好了

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 恶意骚扰扣12分怎么办 新店开张交保证金被骗了怎么办 支付宝蚂蚁花呗逾期怎么办 被注销的微信怎么办 花呗有些不能用怎么办 实体店生意不好做怎么办 电器实体店生意越来越差怎么办 开业第一天不吉利怎么办 美容店开业第一天没人怎么办 淘宝店铺没有人访问怎么办 淘宝店铺没有人问怎么办 淘宝申请退款后店铺关闭怎么办 宝贝详情怎么改不了怎么办 改详情页后被删除宝贝怎么办 淘宝网商贷生意不好还不了怎么办 英国遗失在酒店物品怎么办 班福法则首位是0怎么办 同事能力比你强怎么办 新买的木板床响怎么办 笔记本键盘驱动坏了怎么办 云柜快递超时了怎么办 毕业设计被老师发现抄的怎么办 地板颜色太深了怎么办 皮质鞋子破皮了怎么办 吃了蜘蛛丝会怎么办 南京高二分班不公平怎么办? 高中分班考试没考好怎么办 实木门上的伸缩缝太深怎么办 mac点关机没反应怎么办 被压倒扁的易拉罐怎么办 白色车漏底漆了怎么办 客厅对着卧室门怎么办 老公不上进还懒怎么办 二胡按弦手指分不开怎么办 酷塑做完后疼痛怎么办 冷冻治疗后水泡破了怎么办 冷冻治疗的水泡破了怎么办? 冷冻治疗水泡破了怎么办 脚上冷冻后起泡怎么办 刺猴冷冻后起泡怎么办 隔壁太吵怎么办阴招