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. MyBatis踩坑之SQLProvider转义字符被删除问题

    目录 踩坑背景 问题描述 原因追踪 解决方案 方法一 方法二 踩坑背景 项目架构:Spring Boot + MyBatis + MySQL. 使用MyBatis作为ORM框架,jdbc驱动使用的是m ...

  2. CentOS7.5 使用 kubeadm 安装配置 Kubernetes1.12(四)

    在之前的文章,我们已经演示了yum 和二进制方式的安装方式,本文我们将用官方推荐的kubeadm来进行安装部署. kubeadm是 Kubernetes 官方提供的用于快速安装Kubernetes集群 ...

  3. 在ensp上配置基于接口地址池的DHCP

    原理 实验模拟 实验拓扑 相关参数 开启dhcp服务 开启接口的dhcp服务功能,指定从接口地址池分配(接口1一样) 配置接口DHCP服务租期(租期默认是一天) 设置保留地址 设置dns 我们在pc1 ...

  4. Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activate 'xxxxx.Controllers.xxxxController'.

    Unable to resolve service for type 'Microsoft.Extensions.Logging.ILogger' while attempting to activa ...

  5. python学习-30 总结

    小结 1.map函数: 处理序列中的每个元素,得到结果是一个‘列表’,该‘列表’元素个数及位置与原来一样 2.filter:遍历序列中的每个元素,判断每个元素得到的布尔值,如果是True则留下来,例如 ...

  6. 论文笔记 : NCF( Neural Collaborative Filtering)

    ABSTRACT 主要点为用MLP来替换传统CF算法中的内积操作来表示用户和物品之间的交互关系. INTRODUCTION NeuCF设计了一个基于神经网络结构的CF模型.文章使用的数据为隐式数据,想 ...

  7. ELK学习笔记之配置logstash消费kafka多个topic并分别生成索引

    0x00 filebeat配置多个topic filebeat.prospectors: - input_type: log encoding: GB2312 # fields_under_root: ...

  8. .net Aop 实现原理

    本文实现所有继承BaseModel的类都通过代理拦截 using System; using System.Reflection; using System.Collections.Generic; ...

  9. Python基础知识(五)------字典

    Python基础知识(四)------字典 字典 一丶什么是字典 ​ dict关键字 , 以 {} 表示, 以key:value形式保存数据 ,每个逗号分隔 ​ 键: 必须是可哈希,(不可变的数据类型 ...

  10. php精度比较函数bccomp

    bccomp (PHP 4, PHP 5, PHP 7) bccomp — 比较两个任意精度的数字 说明 int bccomp ( string $left_operand , string $rig ...