1. 程式人生 > >springboot整合mybatis及封裝curd操作-配置文件

springboot整合mybatis及封裝curd操作-配置文件

enabled () init tps github mys tde oos maven

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

springboot整合mybatis及封裝curd操作-配置文件