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. Ubuntu上使用Docker打包镜像

    关于这个一开始会有点懵,直白一点就是,把本地路径下的代码放到docker里面去,然后在docker这个隔离环境中运行调用我们的程序.专业解释请自行检索学习. 第一步:创建容器 docker run - ...

  2. 007 SpringCloud 学习笔记3-----Eureka注册中心

    1.Eureka概述 (1)引子 网约车出现以前,人们出门叫车只能叫出租车.一些私家车想做出租却没有资格,被称为黑车.而很多人想要约车,但是无奈出租车太少,不方便.私家车很多却不敢拦,而且满大街的车, ...

  3. MySQL单机优化---分表、分区、分库

    一.分表: 水平分表:根据条件把数据分为N个表(例如:商品表中有月份列,则可以按月份进行水平分表). 使用场景:一张表中数据太多,查询效率太慢. 当需要同时查询被水平分表的多张表时: 在两条SQL语句 ...

  4. C++用new与不用new创建对象的区别

    C++创建对象 一.Alignment问题 重新发现这个问题是因为在体系结构课上提到的一个概念,alignment对齐的概念. class MyClass { public : char c; // ...

  5. 全能中间件v19.5.7 正式版发布

    v19.5.7 更新=========================1.新增 支持更多微信公众号API.2.优化 AccessToken 刷新机制.3.修复 微信公众号“消息加解密方式”为“安全模式 ...

  6. python3:使用for循环打印九九乘法表

    for i in range(1, 10): for j in range(1, i + 1): print(j, '*', i, '=', i * j, end=" ") #en ...

  7. 转!!DBCP2 配置详解说明

    转自:https://www.cnblogs.com/diyunpeng/p/6980098.html 由于commons-dbcp所用的连接池出现版本升级,因此commons-dbcp2中的数据库池 ...

  8. 女性对DeepNude脱衣技术的防护

    写在前面的话 本文不提供下载方式,开源部分只是社区逆向后公开的部分源码 这篇文章有些人看了可能会比较极端,但不从技术角度分析又谈何防护?攻与防一直存在,不管是安全还是AI都是一样 你极端不极端,它就在 ...

  9. 2、在NET中实现多线程

    1.System.Threading命名空间 System.Threading命名空间提供了使得可以多线程编程的类和接口 其中 (1)Thread类构成了C#多线程编程的支柱,他用于创建并控制线程 ( ...

  10. Mysql把一个表的数据写入另一个表中

    一.表结构一样 insert into 表1 select * from 表2 二. 表结构不一样或者取部分列 insert into 表1 (列名1,列名2,列名3) select 列1,列2,列3 ...