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,3.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

时间: 2024-10-26 23:32:08

springboot整合mybatis及封装curd操作-配置文件的相关文章

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

SpringBoot整合mybatis分页操作 SpringBoot整合Mybatis进行分页操作,这里需要使用Mybatis的分页插件:pageHelper, 关于pageHelper的介绍,请查看官方文档: https://pagehelper.github.io/ 1.使用前配置 关于pageHelper的使用配置,主要有以下2个步骤: 1.1.在pom文件中导入pageHelper依赖 <dependency> <groupId>com.github.pagehelper&

SpringBoot整合Mybatis【非注解版】

接上文:SpringBoot整合Mybatis[注解版] 一.项目创建 新建一个工程 ? 选择Spring Initializr,配置JDK版本 ? 输入项目名 ? 选择构建web项目所需的staters(启动器) ? 选择与数据库相关的组件 ? 分析:Spring Boot基本上将我们实际项目开发中所遇到的所有场景都做了封装.它将所有的功能场景都抽取出来,做成了一个个的staters(启动器),只需要在项目的pom.xml配置文件里面引入这些starter相关场景的所有依赖都会导入进来.需要什

springboot整合mybatis(SSM开发环境搭建)

0.项目结构: 1.application.properties中配置整合mybatis的配置文件.mybatis扫描别名的基本包与数据源 server.port=80 logging.level.org.springframework=DEBUG #springboot mybatis #jiazai mybatis peizhiwenjian mybatis.mapper-locations = classpath:mapper/*Mapper.xml mybatis.config-loca

springboot整合mybatis通用Mapper

参考: https://blog.csdn.net/x18707731829/article/details/82814095 https://www.jianshu.com/p/6d2103451dcb SpringBoot整合MyBatis Generator可以帮助我们快速生成实体类.接口.mapper.xml文件,可以提高开发的效率,但是每次新增表都要执行一次Generator生成相应的代码,这样重复的操作生成增删查改重复的的代码,有没有一种通用的接口,不用生成mapper.xml文件,

练习小记2:SpringBoot整合MyBatis

本文主要介绍SpringBoot整合MyBatis的初步过程以及需要注意的细节. SpringBoot简介 官网https://spring.io/projects/spring-boot/ ? MyBatis简介 官网:https://mybatis.org/mybatis-3/zh/index.html MyBatis的优势: 历史发展 MyBatis是由Ibatis发展而来的,Ibatis1.x和Ibatis2.x,都称为Ibatis,在Ibatis3.x版本及以后都称为:MyBatis

SpringBoot 2.SpringBoot整合Mybatis

一.创建Springboot的配置文件:application.properties SpringApplication 会从 application.properties 文件中加载配置信息,下面是添加Spring配置信息的文件目录顺序: 当前目录下的/config子目录中 当前目录中 一个 classpath 包下的 /config 目录中 classpath 根目录中 大家根据自己习惯来即可. /application.properties 文件配置如下: spring.datasourc

SpringBoot整合Mybatis多数据源 (AOP+注解)

SpringBoot整合Mybatis多数据源 (AOP+注解) 1.pom.xml文件(开发用的JDK 10) <?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:s

SpringBoot整合mybatis、shiro、redis实现基于数据库的细粒度动态权限管理系统

1.前言本文主要介绍使用SpringBoot与shiro实现基于数据库的细粒度动态权限管理系统实例. 使用技术:SpringBoot.mybatis.shiro.thymeleaf.pagehelper.Mapper插件.druid.dataTables.ztree.jQuery 开发工具:intellij idea 数据库:mysql.redis 2.表结构还是是用标准的5张表来展现权限.如下图:image 分别为用户表,角色表,资源表,用户角色表,角色资源表.在这个demo中使用了mybat

springboot整合mybatis+oracle

第一步 认识springboot :springboot是为了解决配置文件多,各个组件不统一的问题,它省去了很多配置文件,同时实现了spring产品的整合. 创建springboot项目:通过选择springinit初始化springboot,我们发现它的pom.xml拥有绝大部分的spring所需要的包. 第二步 打开项目的结构,发现有了有趣的部分 在原有的java结构上,公司名称下多了一级叫做自己的项目名的一个目录,这个目录下才是相应的Controlller等层,而且在此目录下面有了一个文件