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 开发,就绝对不可能缺少数据层操作,所有的开发 ...
随机推荐
- 🐯 php项目中类的自动加载
主要函数:spl_autoload_register() — 注册给定的函数作为 __autoload() 的实现 将函数注册到SPL __autoload函数队列中.如果该队列中的函数尚未激活,则激 ...
- 使用HOSTNAMECTL配置主机名
hostnamectl工具是用来管理给定主机中. 查看所有主机名 请运行下面的命令查看所有当前主机名: 〜] $ hostnamectl status 如果未指定任何选项,默认则使用status选项对 ...
- [原创]K8Cscan for Python 2.0
0x000 简介 K8Cscan扫描器Python版支持Windows和Linux系统 详情参考:https://www.cnblogs.com/k8gege/p/10519321.html 0x00 ...
- centos个性化命令行提示符
为了在满屏的命令中找到用户的命令行,所以很有必要设置一种字体颜色.我就设置最实用的一种,可以用蓝色字体显示当前所在路径 命令行输入: echo "PS1='[\${debian_chroot ...
- API开放平台基于accessToken实现
A企业和B企业要进行合作时,A要开放api接口给B调用,这时候A可以采用基于accessToken的方式实现开放api接口 数据库表设计 B调用方式 B企业调用接口前先获取accessToken ht ...
- Java学习之旅(二):生病的狗2(java例化)
废话不多说,直接上肝货,可运行. 代码简陋,逻辑关系可能还不是很严谨,欢迎交流. public class Owner { //属性部分 //狗主人肯定有一条狗,这条狗可以被别的主人检查,所以设置为p ...
- CF-Educational Codeforces Round 77 (Rated for Div. 2)(A-E题解)
A. Heating (水题) 题目链接 大致思路: 因为是代价是平方,所以让每一个房间的大小平均即可,即最大和最小相差不超过一. 代码: #include<bits/stdc++.h> ...
- c++ pipe实现父子进程通信
1.父子进程通信pipe编程流程 -创建管道 -设置进程的输出到管道 -创建进程 -关闭管道写句柄 -读管道读句柄,把数据读到一个buffer里 2.注意事项 -读管道数据的时候,一定要关闭写句柄: ...
- day04——列表、元组、range
day04 列表 列表--list 有序,可变,支持索引 列表:存储数据,支持的数据类型很多:字符串,数字,布尔值,列表,集合,元组,字典,用逗号分割的是一个元素 id() :获取对象的内存地址 ...
- 深度学习-Wasserstein GAN论文理解笔记
GAN存在问题 训练困难,G和D多次尝试没有稳定性,Loss无法知道能否优化,生成样本单一,改进方案靠暴力尝试 WGAN GAN的Loss函数选择不合适,使模型容易面临梯度消失,梯度不稳定,优化目标不 ...