2017.12.19 MyBatis批量修改、项目搭建

一、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.结构和主程序
结构和主程序

主程序中

@SpringBootApplication
public 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

repository:
@Repository

使用REST Client时
在Request的Headers中加入
Content-Type:application/json
Cookie:

使用@Data 需要lombok 到maven repository

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值