2017.12.19 MyBatis批量修改、项目搭建

来源:互联网 发布:智能证件照相软件 编辑:程序博客网 时间:2024/05/22 03:10

一、MyBatis批量修改
1.修改的字段都有相同的值

<update id="update">    //链表中元素的类型    update table1   set             id=#{id},            city=#{city}        where city_id in         <foreach collection="cityIdList" index="index" item="cityId" pen="("separator=","close=")">    // 链表打开的形式前后括号,中间逗号分隔         #{cityId}        </foreach>  </update>  

2.修改的字段值不同

<update id="batchUpdate"  parameterType="java.util.List">            <foreach collection="list" item="workCard" index="index" open="" close="" separator=";">              update table2               <set>                               city=#{workCard.id},              city_id=#{workCard.cityId}              </set>              where id = ${workCard.id}       </foreach>        </update>  

3.set和set标签
当没有if判断句,直接用set,有if时要用和标签。

二、项目搭建
1.创建maven项目
创建maven项目

2.删除本来的,创建module
创建module

3.到spring boot官网得到

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.9.RELEASE</version></parent><dependencies><dependency>    <groupId>org.springframework.boot</groupId>    <artifactId>spring-boot-starter-web</artifactId></dependency></dependencies>

parent放在最外的pom.xml
第二个放在内部的pom.xml,jar

4.结构和主程序
结构和主程序

主程序中

@SpringBootApplicationpublic class ServerApplication {public static void main(String[] args) {    SpringApplication.run(ServerApplication.class,args);}}

5.MyBatis
①内部pom.xml中

    <dependency>        <groupId>org.mybatis.spring.boot</groupId>        <artifactId>mybatis-spring-boot-starter</artifactId>    </dependency>    <dependency>        <groupId>mysql</groupId>        <artifactId>mysql-connector-java</artifactId>        <version>5.1.42</version>    </dependency>

②在resources下创建文件 application.properties

mybatis.config-location=/mybatis.xml
mybatis.mapperLocations=classpath:mapper/*.xml
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxxxxxxx?useUnicode=true&characterEncoding=UTF8&serverTimezone=UTC&allowMultiQueries=true
spring.datasource.username=xxxxx
spring.datasource.password=xxxxx
数据库名 账号 密码

在与properties相同位置可以创建mybatis.xml进行Mybatis的设置。

<?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><environments default="development">    <environment id="development">        <transactionManager type="JDBC"/>        <dataSource type="POOLED">            <property name="driver" value="${driver}"/>            <property name="url" value="${url}"/>            <property name="username" value="${username}"/>            <property name="password" value="${password}"/>        </dataSource>    </environment></environments></configuration>

要注意 XML 头部的声明,用来验证 XML 文档正确性。environment 元素体中包含了事务管理和连接池的配置。

③resources下创建mapper包,里面放xml
domain下放对应的数据类 (@Data需要增加依赖,spring repository)
repository放接口 @Repository
xml

在xml下拼装sql语句,在repository下的mapper中定义接口

controller :
@RestController
@RequestMapping(“/application”)

service接口
serviceImpl:
@Service