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() == "&lt;" || val.toString() == "&gt;"
|| val.toString() == "&lt;=" || val.toString() == "&gt;=" || 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() == "&lt;" || val.toString() == "&gt;"
|| val.toString() == "&lt;=" || val.toString() == "&gt;=" || 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() == "&lt;" || val.toString() == "&gt;"
|| val.toString() == "&lt;=" || val.toString() == "&gt;=" || 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() == "&lt;" || val.toString() == "&gt;"
|| val.toString() == "&lt;=" || val.toString() == "&gt;=" || 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() == "&lt;" || val.toString() == "&gt;"
|| val.toString() == "&lt;=" || val.toString() == "&gt;=" || 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() == "&lt;" || val.toString() == "&gt;"
|| val.toString() == "&lt;=" || val.toString() == "&gt;=" || 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操作-配置文件的更多相关文章

  1. SpringBoot整合Mybatis对单表的增、删、改、查操作

    一.目标 SpringBoot整合Mybatis对单表的增.删.改.查操作 二.开发工具及项目环境 IDE: IntelliJ IDEA 2019.3 SQL:Navicat for MySQL 三. ...

  2. Springboot整合Mybatis实现级联一对多CRUD操作

    在关系型数据库中,随处可见表之间的连接,对级联的表进行增删改查也是程序员必备的基础技能.关于Spring Boot整合Mybatis在之前已经详细写过,不熟悉的可以回顾Spring Boot整合Myb ...

  3. SpringBoot整合mybatis使用pageHelper插件进行分页操作

    SpringBoot整合mybatis分页操作 SpringBoot整合Mybatis进行分页操作,这里需要使用Mybatis的分页插件:pageHelper, 关于pageHelper的介绍,请查看 ...

  4. Spring Boot整合Mybatis并完成CRUD操作

    MyBatis 是一款优秀的持久层框架,被各大互联网公司使用,本文使用Spring Boot整合Mybatis,并完成CRUD操作. 为什么要使用Mybatis?我们需要掌握Mybatis吗? 说的官 ...

  5. SpringBoot整合Mybatis【非注解版】

    接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ​ 选择Spring Initializr,配置JDK版本 ​ 输入项目名 ​ 选择构建web项目所需的state ...

  6. SpringBoot整合Mybatis之xml

    SpringBoot整合Mybatis mybatis ORM框架.几个重要的概念: Mapper配置 : 可以使用基于XML的Mapper配置文件来实现,也可以使用基于Java注解的Mybatis注 ...

  7. 【java框架】SpringBoot(7) -- SpringBoot整合MyBatis

    1.整合MyBatis操作 前面一篇提到了SpringBoot整合基础的数据源JDBC.Druid操作,实际项目中更常用的还是MyBatis框架,而SpringBoot整合MyBatis进行CRUD也 ...

  8. springboot学习随笔(四):Springboot整合mybatis(含generator自动生成代码)

    这章我们将通过springboot整合mybatis来操作数据库 以下内容分为两部分,一部分主要介绍generator自动生成代码,生成model.dao层接口.dao接口对应的sql配置文件 第一部 ...

  9. SpringBoot系列七:SpringBoot 整合 MyBatis(配置 druid 数据源、配置 MyBatis、事务控制、druid 监控)

    1.概念:SpringBoot 整合 MyBatis 2.背景 SpringBoot 得到最终效果是一个简化到极致的 WEB 开发,但是只要牵扯到 WEB 开发,就绝对不可能缺少数据层操作,所有的开发 ...

随机推荐

  1. 🐯 php项目中类的自动加载

    主要函数:spl_autoload_register() — 注册给定的函数作为 __autoload() 的实现 将函数注册到SPL __autoload函数队列中.如果该队列中的函数尚未激活,则激 ...

  2. [QT] - 图片查看器(数码相框)#工程源码

    简介: 大学期间弄的一个小软件,当时是将该软件移植到嵌入式开发板使用的,软件的几个功能截图如正文所示,文末提供工程源码文件,感谢支持! 功能截图: [ 打开软件 ] [ 打开图片 ] [ 图片可放大. ...

  3. 彻底理解Runnable和Thread的区别

    昨天去面试,面试官问了一个问题:Runnable和Thread有什么区别,因为针对这个问题以前有背过,并且网上大多数都是这些结论,所以脱口而出: 1.Thread有单继承的问题: 2.Runnable ...

  4. 嵌入式02 STM32 实验10 定时器中断

    优秀文章 https://blog.csdn.net/qq_38351824/article/details/82619734 一.STM32通用定时器(TIM2.TIM3.TIM4和TIM5共四个通 ...

  5. centos实现三个节点高可用

    centos实现三个节点高可用 使用的资源为keepalived和nginx 高可用主机IP地址 192.168.136.131 192.168.136.133 192.168.136.134 ngi ...

  6. DRF框架(一)——restful接口规范、基于规范下使用原生django接口查询和增加、原生Django CBV请求生命周期源码分析、drf请求生命周期源码分析、请求模块request、渲染模块render

    DRF框架    全称:django-rest framework 知识点 1.接口:什么是接口.restful接口规范 2.CBV生命周期源码 - 基于restful规范下的CBV接口 3.请求组件 ...

  7. c语言 判断字符串长度 实现

    /* 首先明白答案的本质(该函数)是一个计数器该计数器用for循环来实现实现对一串字符串的计数字符串以空格开头 不计算空格 计算空格后的数字直到遇到\0结束.num计算器字符串不以空格结束 计算空格后 ...

  8. Golang-使用mysql

    一.安装mysql-driver驱动 go get github.com/go-sql-driver/mysql 二.安装完毕之后,就可以通过go语言操作mysql了 const ( _selectU ...

  9. SQL SERVER 查询所有表大小

    DECLARE @T TABLE ( [name] VARCHAR(max), [rows] INT, reserved VARCHAR(max), data_size VARCHAR(max), i ...

  10. Java线程本地存储ThreadLocal

    前言 ThreadLocal 是一种 无同步 的线程安全实现 体现了 Thread-Specific Storage 模式:即使只有一个入口,内部也会为每个线程分配特有的存储空间,线程间 没有共享资源 ...