springboot聚合工程之添加mybatis数据库持久化操作
在boot-polymer-repository工程添加mybatis的相关依赖
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>com.polymer</groupId> <artifactId>boot-polymer</artifactId> <version>1.0.0.RELEASE</version> </parent> <artifactId>boot-polymer-repository</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mybaits配置 --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.2.0</version> </dependency> <!-- 添加druid使用阿里巴巴druid连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.28</version> </dependency> <!-- 添加mysql驱动 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--dbutils --> <dependency> <groupId>commons-dbutils</groupId> <artifactId>commons-dbutils</artifactId> <version>1.7</version> </dependency> </dependencies> </project>
添加application配置本项目使用的是yml文件
spring: datasource: name: test url: jdbc:mysql://localhost:3306/epay?characterEncoding=UTF-8 username: root password: 123 # 使用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: mapperLocations: classpath:mapper/*.xml typeAliasesPackage: com.polymer.app.entity
第三步:使用mybatis自动生成工具将表生成mapper文件
CREATE TABLE `bussiness_param` (
`PARAM_TYPE` varchar(20) NOT NULL DEFAULT ‘‘ COMMENT ‘类型‘,
`PARAM_VALUE` varchar(255) NOT NULL COMMENT ‘值‘,
`DESCR` varchar(255) DEFAULT NULL COMMENT ‘说明‘,
`REMARK` varchar(255) DEFAULT NULL COMMENT ‘备注‘,
PRIMARY KEY (`PARAM_TYPE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
package com.polymer.app.entity; public class BussinessDO { private String paramType; private String paramValue; private String descr; private String remark; public String getParamType() { return paramType; } public void setParamType(String paramType) { this.paramType = paramType; } public String getParamValue() { return paramValue; } public void setParamValue(String paramValue) { this.paramValue = paramValue; } public String getDescr() { return descr; } public void setDescr(String descr) { this.descr = descr; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } }
package com.polymer.app.mapper; import org.apache.ibatis.annotations.Mapper; import com.polymer.app.entity.BussinessDO; @Mapper public interface BussinessMapper { int deleteByPrimaryKey(String paramType); int insert(BussinessDO record); int insertSelective(BussinessDO record); BussinessDO selectByPrimaryKey(String paramType); int updateByPrimaryKeySelective(BussinessDO record); int updateByPrimaryKey(BussinessDO record); }
<?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.polymer.app.mapper.BussinessMapper"> <resultMap id="BaseResultMap" type="com.polymer.app.entity.BussinessDO"> <id column="PARAM_TYPE" jdbcType="VARCHAR" property="paramType" /> <result column="PARAM_VALUE" jdbcType="VARCHAR" property="paramValue" /> <result column="DESCR" jdbcType="VARCHAR" property="descr" /> <result column="REMARK" jdbcType="VARCHAR" property="remark" /> </resultMap> <sql id="Base_Column_List"> PARAM_TYPE, PARAM_VALUE, DESCR, REMARK </sql> <select id="selectByPrimaryKey" parameterType="java.lang.String" resultMap="BaseResultMap"> select <include refid="Base_Column_List" /> from bussiness_param where PARAM_TYPE = #{paramType,jdbcType=VARCHAR} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.String"> delete from bussiness_param where PARAM_TYPE = #{paramType,jdbcType=VARCHAR} </delete> <insert id="insert" parameterType="com.polymer.app.entity.BussinessDO"> insert into bussiness_param (PARAM_TYPE, PARAM_VALUE, DESCR, REMARK) values (#{paramType,jdbcType=VARCHAR}, #{paramValue,jdbcType=VARCHAR}, #{descr,jdbcType=VARCHAR}, #{remark,jdbcType=VARCHAR}) </insert> <insert id="insertSelective" parameterType="com.polymer.app.entity.BussinessDO"> insert into bussiness_param <trim prefix="(" suffix=")" suffixOverrides=","> <if test="paramType != null"> PARAM_TYPE, </if> <if test="paramValue != null"> PARAM_VALUE, </if> <if test="descr != null"> DESCR, </if> <if test="remark != null"> REMARK, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides=","> <if test="paramType != null"> #{paramType,jdbcType=VARCHAR}, </if> <if test="paramValue != null"> #{paramValue,jdbcType=VARCHAR}, </if> <if test="descr != null"> #{descr,jdbcType=VARCHAR}, </if> <if test="remark != null"> #{remark,jdbcType=VARCHAR}, </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.polymer.app.entity.BussinessDO"> update bussiness_param <set> <if test="paramValue != null"> PARAM_VALUE = #{paramValue,jdbcType=VARCHAR}, </if> <if test="descr != null"> DESCR = #{descr,jdbcType=VARCHAR}, </if> <if test="remark != null"> REMARK = #{remark,jdbcType=VARCHAR}, </if> </set> where PARAM_TYPE = #{paramType,jdbcType=VARCHAR} </update> <update id="updateByPrimaryKey" parameterType="com.polymer.app.entity.BussinessDO"> update bussiness_param set PARAM_VALUE = #{paramValue,jdbcType=VARCHAR}, DESCR = #{descr,jdbcType=VARCHAR}, REMARK = #{remark,jdbcType=VARCHAR} where PARAM_TYPE = #{paramType,jdbcType=VARCHAR} </update> </mapper>
把文件放到以下目录:
后面就可以试着调方法了。注意要把mapper放在对应位置,否则yml配置的路径就找不到了
外贴一个我的项目地址,有兴趣去看一下,后面会陆续完善:https://gitee.com/zyf6303/boot-ployer
原文地址:https://www.cnblogs.com/zyf-yxm/p/10320828.html
时间: 2024-10-09 03:17:01