SpringBoot整合jdbc和事务

SpringBoot整合SSM

0准备工作

配置mysql版本5.7.13,执行安装–默认方式安装—开机自动启动勾选项去掉即可—配置高级环境变量path下mysql安装目录
下载SQL书写软件-Novicat for mysql,常用功能如下:

创建database、创建表格(最右边key)、查询表(查询-新建查询,可写SQL语句)

Hicaricp数据库连接池的配置在pom.xml中(Maven创建SpringBoot项目,见上篇博客)

1.通用mapper配置Mybatis

1).pom.xml中配置通用mapper(通用mapper中含有mybatis和jdbc-Hicaricp)
`<dependency>
        <groupId>tk.mybatis</groupId>
        <artifactId>mapper-spring-boot-starter</artifactId>
        <version>2.0.2</version>
    </dependency>`
2).pom.xml中配置mysql
`<dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>`
3).resources包下application.yaml中配置数据库连接
spring:
  datasource:
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mydatabase
    username: root
    password: xxxx
4).resources包下application.yaml中配置mybatis
mybatis:
  configuration:
    map-underscore-to-camel-case: true
  # mapper-locations: mapper/*.xml//扫描mapper包下所有
  type-aliases-package: tortoise(项目名).pojo(包名,存放表对应的对象类)#包名
5).mapper包下的UserMapper类
package tortoise.mapper;
import tortoise.pojo.User;
public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User>{

}
6).写测试类
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
package tortoise.mapper;

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;
import tortoise.pojo.User;
@RunWith(SpringRunner.class)
@SpringBootTest
/*
* 测试类!!!
* */
public class UserMapperTest{
    @Autowired
    private UserMapper userMapper;
    @Test
    public void query(){
        User user = userMapper.selectByPrimaryKey(2);
        System.out.println(user);
    }
}
7).pojo包下的对象类
package tortoise.pojo;
import lombok.Data;
import tk.mybatis.mapper.annotation.KeySql;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
@Data//自动生成setter和getter和hashcode和toString方法,是lombok插件(具体下载和配置见上博客)
@Table(name = "user")//表名
public class User {
    @Id//主键ID
    @KeySql(useGeneratedKeys = true)//自增
    private Integer id;//!!!通用mapper,不识别int
    private String name;
    private Integer age;//!!!通用mapper,不识别int
    @Transient//表明下面属性,非表中属性
    private String address;
}

2.service

package tortoise.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tortoise.mapper.UserMapper;
import tortoise.pojo.User;
@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    @Transactional//!!!事务注解的配置
    public User queryById(int id){
        return userMapper.selectByPrimaryKey(id);
    }
}

3.web

package tortoise.web;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tortoise.pojo.User;
import tortoise.service.UserService;
@RestController
@Slf4j//lombok插件中的打印日志
@RequestMapping("user")//路径(   localhost:8080/user/id=1,2,3````)
public class MyController {
    @Autowired
    private UserService userService;
    @GetMapping("{id}")
    public User hello(@PathVariable("id") int id){
        User user = userService.queryById(id);
        return user;
    }

4.启动

package tortoise;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication//SpringBoot核心
@MapperScan("tortoise.mapper")//mybatis配置mapper接口
public class MyFirstDemo {
    public static void main(String[] args) {
        SpringApplication.run(MyFirstDemo.class,args);
    }
}

5.强调

通用mapper不识别int,可以将User类中int id改为Integer/Long id
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值