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就好了
阅读全文
0 0
- springboot从零开始[2] gradle+mybatis xml
- springboot从零开始[1] gradle+mybatis注解
- MyBatis springboot gradle 开发配置
- springboot-gradle + mybatis-generator-plugin
- SpringBoot(三):springboot整合mybatis(xml形式)
- springboot 使用mybatis (xml Mapper)
- springboot集成mybatis xml方式
- springboot整合mybatis(xml版)
- 从零开始springBoot
- springboot+mybatis+mysql(2)
- springboot整合mybatis xml方式配置SQL
- IDEA下创建Springboot,thymeleaf,Mybatis,Postgresql,Gradle项目
- springboot+mybatis+gradle在idea和oracle使用
- Gradle构建SpringBoot+Mybatis集成分页插件PageHelper
- SOA微服务案例springboot+mybatis使用gradle构建案例
- gradle+springBoot
- springboot笔记(2)springboot-Mybatis使用
- 从零开始的Android新项目2 - Gradle篇
- HBase 数据及物理模型 架构及工作原理
- python3常见编码转换
- butterknife总结(一)
- 简单介绍当前Windows支持的各种Socket I/O模型
- tensorflow学习笔记(2):常量(tf.constant)与变量(tf.Varialbe)
- springboot从零开始[2] gradle+mybatis xml
- 【剑指offer】面试题66:构建乘积数组
- mysql如何解决中文编码问题
- Java设计模式之创建型模式-建造者模式(Builder)
- Django笔记1——安装配置(Linux环境)
- 判断两条线段是否相交
- 机器学习基础-学习笔记 概率论
- JavaScript创建对象
- Bootstrap学习