springboot整合mybatis

pom.xml文件的依赖配置

<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>8.0.19</version>
</dependency>
<!--alibaba的druid数据库连接池-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.21</version>
</dependency>
com.github.pagehelper pagehelper-spring-boot-starter 1.2.13 配置application.yml文件 spring: datasource: name: ssm url: jdbc:mysql://127.0.0.1:3306/ssm data-username: root data-password: root type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.jdbc.Driver druid: filters: stat max-active: 20 initial-size: 1 max-wait: 60000 min-idle: 1 time-between-eviction-runs-millis: 60000 min-evictable-idle-time-millis: 30000 validation-query: select 'x' test-while-idle: true test-on-borrow: false test-on-return: false pool-prepared-statements: true max-open-prepared-statements: 20 #mybatis mybatis: mapper-locations: classpath:mapping/.xml config-location: classpath:mybatis/mybqtis-config.xml #配置分页设置 pagehelper: dialect: mysql reasonable: true support-methods-arguments: true params: count = countSql 在resource下面创建mybatis的包,包下创建mybatis-config.xml文件: <?xml version="1.0" encoding="UTF-8"?>
        </dataSource>
    </environment>
</environments>
\剩下就是实体类层,在main.java下创建com.ss.pojo package pojo;

public class Active {
private Integer id;
private String name;
private double money;

public Active() {
    this.id = id;
    this.name = name;
    this.money = money;
}

public Integer getId() {
    return id;
}

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

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public double getMoney() {
    return money;
}

public void setMoney(double money) {
    this.money = money;
}

}
然后创建它的功能实现mapper映射方法在mapper下创建
package mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Service;
import pojo.Active;

public interface UserMapper {
@Select ( “select * from account where name = #{name}” )
Active findUserName(@Param ( “name” )String name);
@Insert ( “insert into account(name,money) values(#{name},#{money})” )
void addUser(@Param ( “name” )String name,@Param ( “money” )double money);
}
在service下创建它的service接口的方法
package service;

import pojo.Active;

public interface UserService {

Active findUser(String name);

void saveUser(Active active);

}
创建实现接口的impl
package service;

import mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import pojo.Active;
@Service
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapper;

@Override
public Active findUser(String name) {
    return userMapper.findUserName ( name );
}

@Override
public void saveUser(Active active) {
userMapper.addUser ( active.getName (),active.getMoney () );
}

}
在controller层是作为前后端调用的位置,这里只说后端,所以就在后端直接给name和money赋值
package controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import pojo.Active;
import service.UserService;

@RestController
public class UserController {
@Autowired
private UserService userService;

@RequestMapping("/findUser")

// @ResponseBody
public Active findUser(String name){
return userService.findUser ( name );
}
@RequestMapping("/adduser")
// @ResponseBody
public String addUser(){
Active active = new Active ();
active.setName ( “asd” );
active.setMoney ( 123 );
userService.saveUser ( active );
return “ok”;
}
}
然后就完成了springboot和mybatis的整合,运行之后直接在网页打开
localhost:8080/adduser
结束

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值