eclipse下spring boot 整合 mybatis
开发环境
1、 eclipse neon
2、 jdk 1.8.0_172
3、 Maven 3.5.3
4、 spring boot 版本 : 2.0.2.RELEASE
5、 mybatis generator 自动生成代码插件
开始
- 步骤1:使用eclipse新建spring boot项目,选择eclipse工具条上的file–>new–> other–>spring boot–>Spring Starer Project;
选择spring starter project然后点击next
将jdbc、mysql、web、mybatis 选上 点击finish这样 一个spring boot项目就新建好了。
注意:第一次新建spring boot项目时,可能没有这些选项,需要安装spring boot 相关的插件,选择工具条上的help–>Eclipse marketplace后选择popular,安装sts重启eclipse后就可以新建工程了。
新建工程中遇到的问题:新建好工程的pom.xml第一行报错,原因有些jar没有下载下来,将maven的 settings.xml文件的地址修改为国内的镜像地址即可解决。
完整的工程目录:
- 步骤2:创建数据库表
创建一张城市表字段有id、city_name、city_code
CREATE TABLE `city` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`city_name` varchar(50) DEFAULT NULL COMMENT '城市名称',
`city_code` varchar(255) DEFAULT NULL COMMENT '地区编码',
PRIMARY KEY (`Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
- 步骤3使用mybatis generator 自动生成mapping、model、mapper
eclipse 安装mybatis generator
在pom.xml文件中添加
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.6</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
</plugin>
generatorConfig.xml配置文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 数据库驱动:选择你的本地硬盘上面的数据库驱动包-->
<classPathEntry location="E:\mysql\mysql-connector-java-5.1.46.jar"/>
<context id="DB2Tables" targetRuntime="MyBatis3">
<commentGenerator>
<property name="suppressDate" value="true"/>
<!-- 是否去除自动生成的注释 true:是 : false:否 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--数据库链接URL,用户名、密码 -->
<jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://127.0.0.1/springboottest" userId="root" password="Qq@123456">
</jdbcConnection>
<javaTypeResolver>
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- 生成模型的包名和位置-->
<javaModelGenerator targetPackage="com.example.demo.model" targetProject="springboot_mybatis_demo/src/main/java">
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 生成映射文件的包名和位置-->
<sqlMapGenerator targetPackage="mapping" targetProject="springboot_mybatis_demo/src/main/resources">
<property name="enableSubPackages" value="true"/>
</sqlMapGenerator>
<!-- 生成DAO的包名和位置-->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.example.demo.mapper" targetProject="springboot_mybatis_demo/src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 要生成的表 tableName是数据库中的表名或视图名 domainObjectName是实体类名-->
<table tableName="city" domainObjectName="City" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"></table>
</context>
</generatorConfiguration>
在generatorConfig.xml配置要生成代码的路径
生成文件
- 步骤4:创建controler(控制器)、service(处理业务逻辑)
1、在controler包下新建一个CityController类,用来处理请求;
/**
* @Title: CityController.java
* @Package com.example.demo.controller
* @Description: TODO(用一句话描述该文件做什么)
* @author wjk
* @date 2018年5月10日
* @version V1.0
*/
package com.example.demo.controller;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.City;
import com.example.demo.service.CityService;
/**
* @ClassName: CityController
* @Description: CityController
* @author wjk
* @date 2018年5月10日
*
*/
@RequestMapping("/city")
@RestController
public class CityController {
@Resource
private CityService cityService;
/**
*
* @Title: insertValus
* @Description: 插入数据
* @param 参数
* @return void 返回类型
* @throws
* @author wjk
*/
@GetMapping("/insertValues")
public void insertValus() {
City city = new City();
city.setCityCode("370000");
city.setCityName("山东");
city.setId(2);
cityService.insertValues(city);
}
/**
*
* @Title: listCity
* @Description: 查询所有的记录
* @param @return 参数
* @return List<City> 返回类型
* @throws
* @author wjk
*/
@GetMapping("/list")
public List<City> listCity() {
return cityService.listCity();
}
/**
*
* @Title: selectByPrimaryKey
* @Description: TODO(这里用一句话描述这个方法的作用)
* @param @return 参数
* @return City 返回类型
* @throws
*/
@RequestMapping("getCityById")
public City selectByPrimaryKey() {
return cityService.selectByPrimaryKey(1);
}
}
2、在service包下新建CityService类用来处理业务逻辑;
/**
* @Title: CityService.java
* @Package com.example.demo.service
* @Description: TODO(用一句话描述该文件做什么)
* @author wjk
* @date 2018年5月10日
* @version V1.0
*/
package com.example.demo.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.mapper.CityMapper;
import com.example.demo.model.City;
/**
* @ClassName: CityService
* @Description: CityService 业务逻辑处理类
* @author wjk
* @date 2018年5月10日
*
*/
@Service
public class CityService {
@Autowired
private CityMapper cityMapper;
/**
*
* @Title: insertValues
* @Description: insert city
* @param @param city 参数
* @return void 返回类型
* @throws
*/
public void insertValues(City city) {
cityMapper.insert(city);
}
/**
* @Title: listCity
* @Description: list all
* @param @return 参数
* @return List<City> 返回类型
* @throws
*/
public List<City> listCity() {
return (List<City>) cityMapper.selelctList();
}
/**
*
* @Title: selectByPrimaryKey
* @Description: find city by id
* @param @param primaryKey
* @param @return 参数
* @return City 返回类型
* @throws
*/
public City selectByPrimaryKey(int primaryKey) {
return cityMapper.selectByPrimaryKey(primaryKey);
}
}
CityMapper 代码
package com.example.demo.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.example.demo.model.City;
@Mapper
public interface CityMapper {
/**
*
* @Title: deleteByPrimaryKey
* @Description: 根据city 表主键删�?? 数据
* @param @param id
* @param @return 参数
* @return int 返回类型
* @throws
* @author wjk
*/
int deleteByPrimaryKey(Integer id);
/**
*
* @Title: insert
* @Description: 插入数据
* @param @param record
* @param @return 参数
* @return int 返回类型
* @throws
* @author wjk
*/
int insert(City record);
/**
*
* @Title: insertSelective
* @Description: TODO(这里用一句话描述这个方法的作�??)
* @param @param record
* @param @return 参数
* @return int 返回类型
* @throws
* @author wjk
*/
int insertSelective(City record);
/**
*
* @Title: selectByPrimaryKey
* @Description: TODO(这里用一句话描述这个方法的作�??)
* @param @param id
* @param @return 参数
* @return City 返回类型
* @throws
* @author wjk
*/
City selectByPrimaryKey(Integer id);
/**
*
* @Title: updateByPrimaryKeySelective
* @Description: TODO(这里用一句话描述这个方法的作�??)
* @param @param record
* @param @return 参数
* @return int 返回类型
* @throws
* @author wjk
*/
int updateByPrimaryKeySelective(City record);
/**
*
* @Title: updateByPrimaryKey
* @Description: TODO(这里用一句话描述这个方法的作�??)
* @param @param record
* @param @return 参数
* @return int 返回类型
* @throws
* @author wjk
*/
int updateByPrimaryKey(City record);
/**
*
* @Title: selelctList
* @Description: TODO(这里用一句话描述这个方法的作�??)
* @param @return 参数
* @return City 返回类型
* @throws
*/
List<City> selelctList();
}
CityMapper.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.demo.mapper.CityMapper">
<resultMap id="BaseResultMap" type="com.example.demo.model.City">
<id column="Id" jdbcType="INTEGER" property="id" />
<result column="city_name" jdbcType="VARCHAR" property="cityName" />
<result column="city_code" jdbcType="VARCHAR" property="cityCode" />
</resultMap>
<sql id="Base_Column_List">
Id, city_name, city_code
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from city
where Id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from city
where Id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.example.demo.model.City">
insert into city (Id, city_name, city_code
)
values (#{id,jdbcType=INTEGER}, #{cityName,jdbcType=VARCHAR}, #{cityCode,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.example.demo.model.City">
insert into city
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
Id,
</if>
<if test="cityName != null">
city_name,
</if>
<if test="cityCode != null">
city_code,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="cityName != null">
#{cityName,jdbcType=VARCHAR},
</if>
<if test="cityCode != null">
#{cityCode,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.demo.model.City">
update city
<set>
<if test="cityName != null">
city_name = #{cityName,jdbcType=VARCHAR},
</if>
<if test="cityCode != null">
city_code = #{cityCode,jdbcType=VARCHAR},
</if>
</set>
where Id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.demo.model.City">
update city
set city_name = #{cityName,jdbcType=VARCHAR},
city_code = #{cityCode,jdbcType=VARCHAR}
where Id = #{id,jdbcType=INTEGER}
</update>
<resultMap id="BaseResultMap" type="com.example.demo.model.City">
<id column="Id" jdbcType="INTEGER" property="id" />
<result column="city_name" jdbcType="VARCHAR" property="cityName" />
<result column="city_code" jdbcType="VARCHAR" property="cityCode" />
</resultMap>
<sql id="Base_Column_List">
Id, city_name, city_code
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from city
where Id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from city
where Id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.example.demo.model.City">
insert into city (Id, city_name, city_code
)
values (#{id,jdbcType=INTEGER}, #{cityName,jdbcType=VARCHAR}, #{cityCode,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.example.demo.model.City">
insert into city
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
Id,
</if>
<if test="cityName != null">
city_name,
</if>
<if test="cityCode != null">
city_code,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="cityName != null">
#{cityName,jdbcType=VARCHAR},
</if>
<if test="cityCode != null">
#{cityCode,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.demo.model.City">
update city
<set>
<if test="cityName != null">
city_name = #{cityName,jdbcType=VARCHAR},
</if>
<if test="cityCode != null">
city_code = #{cityCode,jdbcType=VARCHAR},
</if>
</set>
where Id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.demo.model.City">
update city
set city_name = #{cityName,jdbcType=VARCHAR},
city_code = #{cityCode,jdbcType=VARCHAR}
where Id = #{id,jdbcType=INTEGER}
</update>
<resultMap id="BaseResultMap" type="com.example.demo.model.City">
<id column="Id" jdbcType="INTEGER" property="id" />
<result column="city_name" jdbcType="VARCHAR" property="cityName" />
<result column="city_code" jdbcType="VARCHAR" property="cityCode" />
</resultMap>
<sql id="Base_Column_List">
Id, city_name, city_code
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from city
where Id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from city
where Id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.example.demo.model.City">
insert into city (Id, city_name, city_code
)
values (#{id,jdbcType=INTEGER}, #{cityName,jdbcType=VARCHAR}, #{cityCode,jdbcType=VARCHAR}
)
</insert>
<insert id="insertSelective" parameterType="com.example.demo.model.City">
insert into city
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
Id,
</if>
<if test="cityName != null">
city_name,
</if>
<if test="cityCode != null">
city_code,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="cityName != null">
#{cityName,jdbcType=VARCHAR},
</if>
<if test="cityCode != null">
#{cityCode,jdbcType=VARCHAR},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.example.demo.model.City">
update city
<set>
<if test="cityName != null">
city_name = #{cityName,jdbcType=VARCHAR},
</if>
<if test="cityCode != null">
city_code = #{cityCode,jdbcType=VARCHAR},
</if>
</set>
where Id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.example.demo.model.City">
update city
set city_name = #{cityName,jdbcType=VARCHAR},
city_code = #{cityCode,jdbcType=VARCHAR}
where Id = #{id,jdbcType=INTEGER}
</update>
<select id="selelctList" resultMap="BaseResultMap">
select * from city
</select>
</mapper>
application.yml 配置
server:
port: 8088
spring:
datasource:
name: localhost
url: jdbc:mysql://localhost:3306/springboottest
username: root
password: Qq@123456
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:mapping/*.xml
type-aliases-package: com.example.demo.model
spring boot 的主程序入口类 文件名为项目名+Application,类里面有@SpringBootApplication 注解 在程序入口上需要加上
@MapperScan("com.example.demo.mapper")
需要将你的mapper 文件扫描进来;到此工程就已经搭建起来了。
- 步骤5 启动工程 测试
访问http:http://localhost:8088/city/insertValues 进行数据的插:
访问:http://localhost:8088/city/list 查看数据,因为使用的是@RestController注解所以界面显示的是 json
@RestController注解相当于@ResponseBody + @Controller合在一起的作用
总结
1、从代码分层、减少耦合度应该将service类定义为接口,新建一个serviceImpl来写具体的