zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    【leetcode】1020. Partition Array Into Three Parts With Equal Sum
    【leetcode】572. Subtree of Another Tree
    【leetcode】123. Best Time to Buy and Sell Stock III
    【leetcode】309. Best Time to Buy and Sell Stock with Cooldown
    【leetcode】714. Best Time to Buy and Sell Stock with Transaction Fee
    【leetcode】467. Unique Substrings in Wraparound String
    【leetcode】823. Binary Trees With Factors
    【leetcode】143. Reorder List
    【leetcode】1014. Capacity To Ship Packages Within D Days
    【leetcode】1013. Pairs of Songs With Total Durations Divisible by 60
  • 原文地址:https://www.cnblogs.com/syscn/p/7506711.html
Copyright © 2011-2022 走看看