Java 微服务 day02 源代码 SpringBoot 实战开发 (五)整合Mybatis(数据库连接池),通用Mapper整合

二、通用Mapper整合


1、引入依赖
(1)在pom.xml当中

在这里插入图片描述

tk.mybatis

mapper-spring-boot-starter

2.0.3

(2)引入的通用Mapper有一些配置就不需要了

在这里插入图片描述

<?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”>

4.0.0

com.itzheng.demo

springboot-demo

1.0.0-SNAPSHOT

spring-boot-starter-parent

org.springframework.boot

2.0.4.RELEASE

org.springframework.boot

spring-boot-starter-web

org.projectlombok

lombok

mysql

mysql-connector-java

tk.mybatis

mapper-spring-boot-starter

2.0.3

(3)驼峰也会默认开启,所以也不需要配置了

在这里插入图片描述

全部

server:

port: 8088

servlet:

path: /

logging:

level:

com.itzheng: debug

#org.springframework: debug

spring:

datasource:

driver-class-name: com.mysql.jdbc.Driver

url: jdbc:mysql://127.0.0.1:3306/itzheng

username: root

password: root

mybatis:

type-aliases-package: com.itzheng.pojo #配置别名包

#mapper-locations: mapper/*.xml

(4)启动类BootDemoApplication改变扫描包通过tk.mybatis.mapper扫描

在这里插入图片描述

在这里插入图片描述

package com.itzheng;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import tk.mybatis.spring.annotation.MapperScan;

@SpringBootApplication

@MapperScan(“com.itzheng.mapper”)

public class BootDemoApplication {

public static void main(String[] args) {

SpringApplication.run(BootDemoApplication.class,args);

}

}

2、创建UserMapper接口并继承Mapper会自动具备一系列的对数据的方法

在这里插入图片描述

package com.itzheng.mapper;

import com.itzheng.pojo.User;

import tk.mybatis.mapper.common.Mapper;

public interface UserMapper extends Mapper {

}

3、在对应的实体类上添加注解实现SQL语句的自动生成

在这里插入图片描述

package com.itzheng.pojo;

import lombok.Data;

import tk.mybatis.mapper.annotation.KeySql;

import javax.persistence.Id;

import javax.persistence.Table;

import javax.persistence.Transient;

import java.util.Date;

@Data

@Table(name=“tb_user”)

public class User {

//id

@Id

@KeySql(useGeneratedKeys = true) //useGeneratedKeys主键自增

private Long id;

//用户名

private String userName;

//密码

private String password;

//姓名

private String name;

//年龄

private Integer age;

//性别 1、男性 2、女性

private Integer sex;

//出生日期

private Date birthday;

//创建时间

private Date created;

//更新时间

private Date updated;

//备注

private String note;

@Transient //Transient当前属性不是要生成SQL的的属性

private int aaaa;

}

4、测试
(1)在pom.xml当中引入test

在这里插入图片描述

org.springframework.boot

spring-boot-starter-test

(2)创建测试类UserMapperTest

在这里插入图片描述

package com.itzheng.mapper;

import com.itzheng.pojo.User;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)

@SpringBootTest

public class UserMapperTest {

@Autowired

private UserMapper userMapper;

@Test

public void testQuery(){

User user = userMapper.selectByPrimaryKey(8L);

System.out.println("User = " +user);

}

}

数据库当中添加一些数据

INSERT INTO tb_user VALUES (‘19’, ‘zhangsan’, ‘123’, ‘zhangsan123’, ‘12’, ‘1’, ‘2021-06-14’, ‘1’, ‘2021-06-29 18:21:50’, ‘2021-06-30 18:21:53’);

在这里插入图片描述

运行测试类

在这里插入图片描述

查询的结果

在这里插入图片描述

三、业务层整合


1、创建UserService

在这里插入图片描述

在这里插入图片描述

package com.itzheng.service;

import com.itzheng.mapper.UserMapper;

import com.itzheng.pojo.User;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

@Service

public class UserService {

@Autowired

private UserMapper userMapper;//注入usermapper接口

//根据id查询的方法

public User queryById(Long id){

return userMapper.selectByPrimaryKey(id);

}

//插入数据的方法

@Transactional //添加事务

public void insertUser(User user){

userMapper.insert(user);

}

}

2、运行测试,
(1)修改HelloController
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值