spring实验6

实验六 Springboot整合Mybatis

目录

实验目的:

实验类型:

实验学时:

实验内容:

实验任务1:创建一个springboot工程导入Spring整合Mybatis相关依赖

实验任务2:同spring实验4一样,创建pojo类,service,controller,dao层

实验任务3:在资源文件下的application.properties文件写入如下代码

实验任务4:mapper映射文件

实验任务5:controller层代码

实验任务6:dao层代码

实验任务7:在浏览器中运行

总结:


实验目的:

  1. 了解Springboot整合Mybatis的作用
  2. 多参数使用注释
  3. 完成Customer增删改查的功能
  4. 使用动态sql完成查询功能

实验类型:

验证性

实验学时:

2学时

实验内容:

实验任务1:创建一个springboot工程导入Spring整合Mybatis相关依赖

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.3.2</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.45</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

</dependencies>

实验任务2:同spring实验4一样,创建pojo类,service,controller,dao层

Pojo代码

public class Customer {
    private Integer id;
    private String username;
    private String jobs;
    private String phone;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getJobs() {
        return jobs;
    }

    public void setJobs(String jobs) {
        this.jobs = jobs;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    @Override
    public String toString() {
        return "Customer{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", jobs='" + jobs + '\'' +
                ", phone='" + phone + '\'' +
                '}';
    }
}

实验任务3:在资源文件下的application.properties文件写入如下代码

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/mybatis?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=x5

# 访问templates包下的文件
spring.web.resources.static-locations=classpath:/templates/,classpath:templates/static/

mybatis.mapper-locations=classpath:mapper/*.xml

实验任务4:mapper映射文件

<?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.li.dao.CustomerDao">
    <select id="query" resultType="com.li.pojo.Customer" >
        select * from customer
        <where>
            <if test="customer.username!=null and customer.username !=''">
               and username like concat('%',#{customer.username},'%')
            </if>

            <if test="customer.jobs!=null and customer.jobs!=''">
                and jobs like concat('%',#{customer.jobs},'%')
            </if>
        </where>

        <if test="pageNo!=null and pageNo!=''">
            limit 0,#{pageNo}
        </if>
    </select>

   <insert id="add" parameterType="com.li.pojo.Customer">
       insert into customer(username,jobs,phone) values(#{username},#{jobs},#{phone});
    </insert>

     <update id="update" parameterType="com.li.pojo.Customer">
        update customer set username=#{username},jobs=#{jobs},phone=#{phone} where id=#{id};
    </update>

    <delete id="delete" parameterType="int">
        delete from customer where id=#{id};
    </delete>
</mapper>

实验任务5:controller层代码

@RestController
public class CustomerController {

    @Autowired
    CustomerService service;

    //1 如果
    //2 如果接受的的参数名不在pojo的属性中 ,那么通过@Param()将值床底到apper.xml中
    @RequestMapping("/query")
    public List<Customer> query(Customer customer,Integer pageNo){//返回集合 springboot 的Controller方法返回值可以自动转json,ssm不行
        List<Customer> list=service.query(customer,pageNo);
        System.out.println(list);
        return list;
    }

    @RequestMapping("/add")
    @Transactional
    public void add(Customer customer){
        customer.setJobs("123");
        customer.setPhone("xxx");
        customer.setUsername("123xx");
        service.add(customer);
    }

    @Transactional
    public void update(Customer customer){
        service.update(customer);
    }

    @Transactional
    public void delete(Integer id){
        service.delete(id);
    }


}

实验任务6:dao层代码

@Component
public interface CustomerDao {

    public List<Customer> query(@Param("customer") Customer customer, @Param("pageNo") Integer pageNo);

    public void add(Customer customer);

    public void update(Customer customer);

    public void delete(Integer id);
}

实验任务7:在浏览器中运行

输入如地址:http://localhost:8080/query

可以查询出全部数据

输入地址:http://localhost:8080/query?username=张

可以看到姓张的数据

 

输入地址:http://localhost:8080/query?username=李&jobs=manager

可以查看到姓李工作是manager的数据

 

总结:

动态sql中的<where>标签可以自动省略掉sql语句前的and

当要传的参数较多时可以使用@Param()注解来指定传的参数名,传参数的方式不止一种,还可以用map等方式来传送参数。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值