spring boot整合mybatis

application.properties

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource.dbcp2.max-idle=10
spring.datasource.dbcp2.max-wait-millis=10000
spring.datasource.dbcp2.min-idle=5
spring.datasource.dbcp2.initial-size=5

server.tomcat.uri-encoding=UTF-8
#配置项目名
server.servlet.context-path=/springboot-1

#加载 mapper.xml 文件
mybatis.mapper-locations=classpath:mapper/xml/*.xml

使用 mybatis 逆向工程生成实体类,mapper ,并配置 service 层

RoomServiceImpl.java

package com.lwj.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lwj.entity.Room;
import com.lwj.mapper.RoomMapper;
import com.lwj.service.RoomService;

@Service
public class RoomServiceImpl implements RoomService{
	
	//Auto inject the mapper interface
	@Autowired
	private RoomMapper roomMapper;

	public Room selectByPrimaryKey(Integer id) {

		return roomMapper.selectByPrimaryKey(id);
	}

	@Transactional    //表示该类需要用到事务,对数据库执行增删改操作时必须开启这个
	public int insert(Room record) {
		return roomMapper.insert(record);
	}

}

RoomController.java

package com.lwj.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 com.lwj.entity.Room;
import com.lwj.service.RoomService;

@Controller
public class RoomController {

	@Autowired
	private RoomService roomService;

	@RequestMapping("/test1.action")
	public String testFunction() {
		
		return "/WEB-INF/jsp/welcome.jsp";
	}

	@RequestMapping("/test2.action")
	public @ResponseBody String testFunction2(Room room) {
		roomService.insert(room);
		return "成功";
	}
}

pom.xml



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

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

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
			<version>2.0.3.RELEASE</version>
		</dependency>

application.java      springboot启动类

package com.lwj;

import javax.sql.DataSource;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Bean;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;

// controller 层必须与 Application 同级或在 Application 级的子目录下 
@SpringBootApplication
@EnableTransactionManagement      //开启事务支持
@MapperScan("com.lwj.mapper")     //扫描 mapper 接口
public class Application extends SpringBootServletInitializer {

	public static void main(String[] args) {

		SpringApplication.run(Application.class,args);
	}
	
	protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
        return application.sources(Application.class);
    }

    @Bean
    public PlatformTransactionManager txManager(DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值