springboot整合mybatis及封装curd操作-配置文件
1 配置文件 application.properties #server server.port=8090 server.address=127.0.0.1 server.session.timeout=1800 server.error.whitelabel.enabled=true #mybatis mybatis.config-locations=classpath:mybatis/mybatis-config.xml // mybatis配置文件 mybatis.mapper-locations=classpath:mybatis/mapper/*.xml //mapper映射文件
mybatis-config.xml <?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>
<!-- 只设置需要的,其他使用默认值 -->
<!-- 开启缓存,默认就是开启的,2层开关,需要在Mapper文件中也指定 cache 标签才会真正使用缓存 -->
<setting name="cacheEnabled" value="true"/>
<!-- 在null时也调用 setter,适应于返回Map,.2版本以上可用 -->
<setting name="callSettersOnNulls" value="true"/>
</settings> <typeAliases>
<typeAlias alias="Integer" type="java.lang.Integer" />
<typeAlias alias="Long" type="java.lang.Long" />
<typeAlias alias="HashMap" type="java.util.HashMap" />
<typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" />
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
<typeAlias alias="LinkedList" type="java.util.LinkedList" />
</typeAliases> </configuration>
BaseMapper.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.sys.mapper.BaseMapper" > <!-- 添加数据 传入map map: 1.table 表名 2.columns 字段 (list) 3.values 字段值 (list) -->
<insert id="save" parameterType="java.util.Map">
insert into ${table}
<foreach collection="columns" item="item1" index="index" open="("
close=")" separator=",">
${item1}
</foreach>
values
<foreach collection="values" item="item2" index="index" open="("
close=")" separator=",">
#{item2}
</foreach>
</insert>
<!-- 添加数据 返回主键 传入map map: 1.table 表名 2.columns 字段 (list) 3.values 字段值 (list) -->
<insert id="saveRetkey" parameterType="java.util.Map"
useGeneratedKeys="true" keyProperty="id">
insert into ${table}
<foreach collection="columns" item="item" index="index" open="("
close=")" separator=",">
${item}
</foreach>
values
<foreach collection="values" item="item" index="index" open="("
close=")" separator=",">
#{item}
</foreach>
</insert> <!-- 修改 传入map map: 1.table 表名 2.columnvalues 字段-值 (map) 3.wheres 条件字段 (map) -->
<update id="update" parameterType="java.util.Map">
update ${table} set
<foreach collection="columnvalues.keys" item="item" index="key"
separator=",">
${item} = #{columnvalues[${item}]}
</foreach>
<where>
<if test="wheres != null">
1=1
<foreach collection="wheres" item="whe" index="index">
<foreach collection="whe.keys" item="item" index="key">
<foreach collection="whe[item]" item="val">
<choose>
<when test='val.toString() == "or" || val.toString() == "and"'>
<![CDATA[${val}]]>
${item}
</when>
<when
test='val.toString() == "=" || val.toString() == "!=" || val.toString() == "<" || val.toString() == ">"
|| val.toString() == "<=" || val.toString() == ">=" || val.toString() == "like"
|| val.toString() == "is null" || val.toString() == "is not null"'>
<![CDATA[ ${val} ]]>
</when>
<otherwise>
#{val}
</otherwise>
</choose>
</foreach>
</foreach>
</foreach>
</if>
</where>
</update> <!-- 删除数据 传入map map: 1.table 表名 2.wheres 条件字段 (map) -->
<delete id="delete" parameterType="java.util.Map">
delete from ${table}
<where>
<if test="wheres != null">
1=1
<foreach collection="wheres" item="whe" index="index">
<foreach collection="whe.keys" item="item" index="key">
<foreach collection="whe[item]" item="val">
<choose>
<when test='val.toString() == "or" || val.toString() == "and"'>
<![CDATA[${val}]]>
${item}
</when>
<when
test='val.toString() == "=" || val.toString() == "!=" || val.toString() == "<" || val.toString() == ">"
|| val.toString() == "<=" || val.toString() == ">=" || val.toString() == "like"
|| val.toString() == "is null" || val.toString() == "is not null"'>
<![CDATA[ ${val} ]]>
</when>
<otherwise>
#{val}
</otherwise>
</choose>
</foreach>
</foreach>
</foreach>
</if>
</where>
</delete> <!-- 删除数据 传入map map: 1.table 表名 2.wheres 条件字段 (map) -->
<delete id="deletes" parameterType="java.util.Map">
delete from ${table}
<where>
<if test="wheres != null">
${idkey} in
<foreach collection="wheres" item="item" index="index" open="("
close=")" separator=",">
#{item}
</foreach>
</if>
</where>
</delete>
<!-- 查询数据根据条件 一个数据 -->
<select id="findByWhere" parameterType="java.util.Map"
resultType="java.util.Map">
select distinct ${columns} from ${table}
<where>
<if test="wheres != null">
1=1
<foreach collection="wheres" item="whe" index="index">
<foreach collection="whe.keys" item="item" index="key">
<foreach collection="whe[item]" item="val">
<choose>
<when test='val.toString() == "or" || val.toString() == "and"'>
<![CDATA[${val}]]>
${item}
</when>
<when
test='val.toString() == "=" || val.toString() == "!=" || val.toString() == "<" || val.toString() == ">"
|| val.toString() == "<=" || val.toString() == ">=" || val.toString() == "like"
|| val.toString() == "is null" || val.toString() == "is not null"'>
<![CDATA[ ${val} ]]>
</when>
<otherwise>
#{val}
</otherwise>
</choose>
</foreach>
</foreach>
</foreach>
</if>
</where>
${sort} limit 1
</select> <!-- 查询所有数据 -->
<select id="findAll" parameterType="java.util.Map" resultType="java.util.Map">
select distinct ${columns} from ${table} ${sort}
</select> <!-- 查询所有数据根据条件 -->
<select id="findAllWhere" parameterType="java.util.Map"
resultType="java.util.Map">
select distinct ${columns} from ${table}
<where>
<if test="wheres != null">
1=1
<foreach collection="wheres" item="whe" index="index">
<foreach collection="whe.keys" item="item" index="key">
<foreach collection="whe[item]" item="val">
<choose>
<when test='val.toString() == "or" || val.toString() == "and"'>
<![CDATA[${val}]]>
${item}
</when>
<when
test='val.toString() == "=" || val.toString() == "!=" || val.toString() == "<" || val.toString() == ">"
|| val.toString() == "<=" || val.toString() == ">=" || val.toString() == "like"
|| val.toString() == "is null" || val.toString() == "is not null"'>
<![CDATA[ ${val} ]]>
</when>
<otherwise>
#{val}
</otherwise>
</choose>
</foreach>
</foreach>
</foreach>
</if>
</where>
${sort}
</select> <!-- 分页查询 -->
<select id="findPageModel" parameterType="java.util.Map"
resultType="java.util.Map">
select distinct ${columns} from ${table}
<where>
<if test="wheres != null">
1=1
<foreach collection="wheres" item="whe" index="index">
<foreach collection="whe.keys" item="item" index="key">
<foreach collection="whe[item]" item="val">
<choose>
<when test='val.toString() == "or" || val.toString() == "and"'>
<![CDATA[${val}]]>
${item}
</when>
<when
test='val.toString() == "=" || val.toString() == "!=" || val.toString() == "<" || val.toString() == ">"
|| val.toString() == "<=" || val.toString() == ">=" || val.toString() == "like"
|| val.toString() == "is null" || val.toString() == "is not null"'>
<![CDATA[ ${val} ]]>
</when>
<otherwise>
#{val}
</otherwise>
</choose>
</foreach>
</foreach>
</foreach>
</if>
</where>
${sort} limit #{pageNo},#{pageSize}
</select> <!-- 总数据条数 -->
<select id="findAllnum" parameterType="java.util.Map"
resultType="int">
select count(a.id) as num from ${table}
<where>
<if test="wheres != null">
1=1
<foreach collection="wheres" item="whe" index="index">
<foreach collection="whe.keys" item="item" index="key">
<foreach collection="whe[item]" item="val">
<choose>
<when test='val.toString() == "or" || val.toString() == "and"'>
<![CDATA[${val}]]>
${item}
</when>
<when
test='val.toString() == "=" || val.toString() == "!=" || val.toString() == "<" || val.toString() == ">"
|| val.toString() == "<=" || val.toString() == ">=" || val.toString() == "like"
|| val.toString() == "is null" || val.toString() == "is not null"'>
<![CDATA[ ${val} ]]>
</when>
<otherwise>
#{val}
</otherwise>
</choose>
</foreach>
</foreach>
</foreach>
</if>
</where>
</select> <!-- 插入数据 直接传入sql -->
<insert id="insertsql" parameterType="String">
<![CDATA[${sql}]]>
</insert>
<!-- 修改数据 直接传入sql -->
<update id="updatesql" parameterType="String">
<![CDATA[${sql}]]>
</update> <!-- 删除数据 直接传入sql -->
<delete id="deletesql" parameterType="String">
<![CDATA[${sql}]]>
</delete> <!-- 查询数据 直接传入sql -->
<select id="selectsqlone" parameterType="String" resultType="java.util.Map">
<![CDATA[${sql}]]>
</select> <!-- 查询数据 直接传入sql -->
<select id="selectsqlall" parameterType="String" resultType="java.util.Map">
<![CDATA[${sql}]]>
</select> <!-- 查询数据 直接传入sql -->
<select id="selectsqlnum" parameterType="String" resultType="int">
<![CDATA[${sql}]]>
</select>
</mapper>
pom.xml <?xml version="1.0" encoding="UTF-8"?>
<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> <groupId>com.example</groupId>
<artifactId>smalldemo</artifactId>
<version>2</version>
<packaging>war</packaging> <name>SpringBootDemo</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.7</java.version>
</properties> <dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis -->
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<version>1.5.6.RELEASE</version>
</dependency> -->
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
<version>1.4.7.RELEASE</version>
</dependency> <dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.16</version>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.38</version>
</dependency> <!--pagehelper-->
<!-- <dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.2</version>
</dependency> -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency> <!-- 微信支付 -->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.3</version>
</dependency> <dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
Application.java /**
* 项目启动类
* spring boot application只会扫描同一包下的类
* @author sys
*
*/
@SpringBootApplication
@EnableAutoConfiguration(exclude = { JacksonAutoConfiguration.class })
@ServletComponentScan
@EnableTransactionManagement //加入事务注解
@MapperScan("com.sys.mapper")
public class Application extends SpringBootServletInitializer{
//fastkson
@Bean
public HttpMessageConverters fastJsonHttpMessageConverters() {
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
FastJsonConfig fastJsonConfig = new FastJsonConfig();
fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat);
fastConverter.setFastJsonConfig(fastJsonConfig);
HttpMessageConverter<?> converter = fastConverter;
return new HttpMessageConverters(converter);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
下篇继续--java对mybatis的curd封装
要源码的联系 qq 2506715686
springboot整合mybatis及封装curd操作-配置文件的更多相关文章
- SpringBoot整合Mybatis对单表的增、删、改、查操作
一.目标 SpringBoot整合Mybatis对单表的增.删.改.查操作 二.开发工具及项目环境 IDE: IntelliJ IDEA 2019.3 SQL:Navicat for MySQL 三. ...
- Springboot整合Mybatis实现级联一对多CRUD操作
在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...
- SpringBoot整合mybatis使用pageHelper插件进行分页操作
SpringBoot整合mybatis分页操作 SpringBoot整合Mybatis进行分页操作,这里需要使用Mybatis的分页插件:pageHelper, 关于pageHelper的介绍,请查看 ...
- Spring Boot整合Mybatis并完成CRUD操作
MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...
- SpringBoot整合Mybatis【非注解版】
接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 选择Spring Initializr,配置JDK版本 输入项目名 选择构建web项目所需的state ...
- SpringBoot整合Mybatis之xml
SpringBoot整合Mybatis mybatis ORM框架.几个重要的概念: Mapper配置 : 可以使用基于XML的Mapper配置文件来实现,也可以使用基于Java注解的Mybatis注 ...
- 【java框架】SpringBoot(7) -- SpringBoot整合MyBatis
1.整合MyBatis操作 前面一篇提到了SpringBoot整合基础的数据源JDBC.Druid操作,实际项目中更常用的还是MyBatis框架,而SpringBoot整合MyBatis进行CRUD也 ...
- springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)
这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...
- SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)
1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...
随机推荐
- Dockerfile常用指令说明
具体解释:https://blog.csdn.net/achenyuan/article/details/80231922 将dockerfile文件和生成好的jar 使用ftp工具上传到linux服 ...
- 基于layUI调用后台数据实现区域信息级联查询
基于layUI调用后台数据实现区域信息级联查询 1.基本思路 后台提供根据区域编码查询区域列表公共接口 页面初始化调用后台接口加载所有省份 点击省份将省份区域编码传入后台查询该省份下所有地市信息,以此 ...
- *** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
Warning提示的原因是 一些未使用的函数被编译进入芯片,浪费了RAM/ROM. 解决的方法: 1.将不用的函数注释: 2.在未使用函数的首尾加条件编译 #ifdef 函数名 和 #endif ,不 ...
- stvd使用中的一些问题
1.stm8_interrupt_vector.c 会莫名其妙的自动出现,而且都是在项目目录下.进行如下操作 2.stvd编译时遇到no default placement for segment . ...
- 用LabVIEW做声源定位系统
前一阵子,研发部举办了为期三天的第一届Innovation Day,让大家用3天时间去完成工作之外的一些创意.有人做微信小程序,有人继续研究一些工作中用到的Tool,有人把一直想解决而没时间解决的老b ...
- python:函数的参数传递方法演示
""" 函数的参数传递方法演示 """ #1.位置传参 def normal(a, b, c): print("1.位置传参:&q ...
- vue-cli中轮播图vue-awesome-swiper使用方法
1 npm 安装 npm install vue-awesome-swiper --save 2在所用的组件中引入 import 'swiper/dist/css/swiper.css' import ...
- Java中守护线程的总结
在Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) 用个比较通俗的比如,任何一个守护线程都是整个JVM中所有非守护线程的保姆: 只要当前JVM实例中尚存 ...
- oracle 数据库触发器,插入更新时间戳
1.首先建立一个测试表 CREATE TABLE TestTragger( UserId int Primary Key, Name VARCHAR() Not Null, CreateTime Ti ...
- 阿里巴巴 Java 开发手册 (五) 集合处理
1. [强制]关于 hashCode 和 equals 的处理,遵循如下规则: 1) 只要重写 equals,就必须重写 hashCode. 2) 因为 Set 存储的是不重复的对象,依据 hashC ...