[SpringBoot]IDEA快速构建SpringBoot应用项目(二)整合Mybatis + Druid
2017-12-23 16:10
1081 查看
配置 pom.xml
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>
hljs xml"> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> <!-- mybatis generator 自动生成代码插件 --> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.2</version> <configuration> <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile> <overwrite>true</overwrite> <verbose>true</verbose> </configuration> </plugin> </plugins> </build>
配置 application.yml
server: port: 8081 spring: datasource: name: test url: jdbc:mysql://101.201.117.102:3306/smb-web?useUnicode=true&useSSL=false&characterEncoding=UTF-8 username: root password: sp6161266 # 使用druid数据源 type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver filters: stat maxActive: 20 initialSize: 1 maxWait: 60000 minIdle: 1 timeBetweenEvictionRunsMillis: 60000 minEvictableIdleTimeMillis: 300000 validationQuery: select 'x' testWhileIdle: true testOnBorrow: false testOnReturn: false poolPreparedStatements: true maxOpenPreparedStatements: 20 mybatis: mapper-locations: classpath:mapper/*.xml type-aliases-package: com.shaonaozu.demo100.*.model
项目完整结构
Test表
CREATE TABLE `test` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(255) DEFAULT '', `content` varchar(255) DEFAULT '', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
Test实体类
package com.shaonaozu.demo100.test.model; public class Test { private Integer id; private String title; private String content; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title == null ? null : title.trim(); } public String getContent() { return content; } public void setContent(String content) { this.content = content == null ? null : content.trim(); } }
TestMapper.java
package com.shaonaozu.demo100.test.dao; import com.shaonaozu.demo100.test.model.Test; import org.springframework.stereotype.Component; @Component public interface TestMapper { int deleteByPrimaryKey(Integer id); int insert(Test record); int insertSelective(Test record); Test selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Test record); int updateByPrimaryKey(Test record); }
TestMapper.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.shaonaozu.demo100.test.dao.TestMapper" > <resultMap id="BaseResultMap" type="com.shaonaozu.demo100.test.model.Test" > <id column="id" property="id" jdbcType="INTEGER" /> <result column="title" property="title" jdbcType="VARCHAR" /> <result column="content" property="content" jdbcType="VARCHAR" /> </resultMap> <sql id="Base_Column_List" > id, title, content </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from test where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from test where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.shaonaozu.demo100.test.model.Test" > insert into test (id, title, content ) values (#{id,jdbcType=INTEGER}, #{title,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR} ) </insert> <insert id="insertSelective" parameterType="com.shaonaozu.demo100.test.model.Test" > insert into test <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="title != null" > title, </if> <if test="content != null" > content, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="title != null" > #{title,jdbcType=VARCHAR}, </if> <if test="content != null" > #{content,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.shaonaozu.demo100.test.model.Test" > update test <set > <if test="title != null" > title = #{title,jdbcType=VARCHAR}, </if> <if test="content != null" > content = #{content,jdbcType=VARCHAR}, </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.shaonaozu.demo100.test.model.Test" > update test set title = #{title,jdbcType=VARCHAR}, content = #{content,jdbcType=VARCHAR} where id = #{id,jdbcType=INTEGER} </update> </mapper>
TestService
package com.shaonaozu.demo100.test.service; import com.shaonaozu.demo100.base.R; import com.shaonaozu.demo100.base.enums.REnums; import com.shaonaozu.demo100.base.exception.RException; import com.shaonaozu.demo100.test.dao.TestMapper; import com.shaonaozu.demo100.test.model.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class TestService { @Autowired private TestMapper testMapper; public R test1(Integer id) throws Exception { R r = new R(); Test test = testMapper.selectByPrimaryKey(id); if (test == null) { throw new RException(REnums.NO_DATA); } return r.setData(test); } public void test2(Test test) throws Exception { testMapper.insertSelective(test); } }
TestServiceTest.java
package com.shaonaozu.demo100.test.service; import com.shaonaozu.demo100.base.R; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class) @SpringBootTest public class TestServiceTest { @Autowired private TestService testService; @Test public void test1() throws Exception { R r = testService.test1(3); System.out.println(r.toString()); // Assert.assertEquals(new Integer(11), new Integer(9)); } @Test public void test2() throws Exception { com.shaonaozu.demo100.test.model.Test test = new com.shaonaozu.demo100.test.model.Test(); test.setTitle("(*^_^*)"); test.setContent("嘻嘻1111111111"); testService.test2(test); } }
执行 test2() 后,数据表结果:
完整代码下载:
http://download.csdn.net/download/alex_bean/10170662
相关文章推荐
- [SpringBoot]IDEA快速构建SpringBoot应用项目(一)
- Spring Boot系列一----使用idea快速构建Spring boot项目
- Springboot整合dubbo构建maven多模块项目(四) - 集成mybatis
- Spring Boot 构建应用——整合 Mybatis
- 初来乍道多多关照,用idea构建springboot+gradle+mybatis+thymeleaf项目
- IDEA上搭建maven项目整合springboot+mybatis+shiro
- 详解SpringBoot 快速整合MyBatis(去XML化)
- Java日志-Spring Boot(1)idea快速构建Spring Boot
- Spring Boot 构建应用——整合消息中间件 Kafka
- 使用 Spring Boot 快速构建 Spring 框架应用
- Maven快速使用教程(二) spring boot 项目构建
- SpringBoot构建微服务实战 之 整合Mybatis(一)
- 快速构建可运行的Spring-boot项目(Hello World)
- SpringBoot基础教程及框架整合(一)----IDEA创建SpringBoot项目
- 使用Spring Boot快速构建应用
- SpringBoot 快速整合Mybatis(去XML化+注解进阶)
- 使用 Spring Boot 快速构建 Spring 框架应用
- 使用Spring Boot快速构建基于SQLite数据源的应用
- 使用Idea创建ssm项目,SpringMVC+Spring+MyBatis+Maven整合