spingboot整合mybatis

本次操作的基础是已经有搭建好的springboot的maven项目

1、在pom.xml文件中加入spingboot和mybaits整合的依赖以及数据库连接的驱动包依赖

<dependency>
	<groupId>mysql</groupId>
	<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis与springboot整合 -->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.0</version>
</dependency>

 2、在main/resources下面增加application.properties文件里面的内容有

#数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///springboot
spring.datasource.username=root
spring.datasource.password=123456

这个是有关数据库连接的4个基本配置,配置到这些就可以正常启动springboot框架了。

接下来继续在application.properties中配置mybatis的一些基本配置

mybatis.typeAliasesPackage=com.test.pojo   //配置别名包
mybatis.mapperLocations=classpath:mappers/*.xml    //扫描到mapper.xml文件
mybatis.configuration.mapUnderscoreToCamelCase=true    //开启驼峰命名规则

3、还需要在启动类上增加一个注解,@MapperScan("com.test.mapper")见名知义,就是开启对mapper接口的扫描

4、这个时候springboot和myabits的简单整合就已经结束,只需要写常规的逻辑代码即可,下面将列出我测试可以正常运行的整合后的所有结构

pom.xml

<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">
	<modelVersion>4.0.0</modelVersion>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.2.RELEASE</version>
		<relativePath />
	</parent>

	<groupId>com.test</groupId>
	<artifactId>springboot-mybatis</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
		</dependency>
		<!--mybatis与springboot整合 -->
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.3.0</version>
		</dependency>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>
</project>

 application.properties

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

mybatis.typeAliasesPackage=com.test.pojo  
mybatis.mapperLocations=classpath:mappers/*.xml 
mybatis.configuration.mapUnderscoreToCamelCase=true 

 启动类,我这次将启动类和控制层写在了一起

import java.util.List;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.test.pojo.User;
import com.test.service.UserService;

@SpringBootApplication
@MapperScan("com.test.mapper")
@Controller
public class StartBoot {

	public static void main(String[] args) {
		SpringApplication.run(StartBoot.class, args);
	}
	
	@Autowired
	private UserService userService;
	
	@RequestMapping("findAll")
	@ResponseBody
	public List<User> findAll(){
		
		return userService.findAll();
		
	}
}

service层

import java.util.List;

import com.test.pojo.User;

public interface UserService {

	List<User> findAll();
}
import java.util.List;

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

import com.test.mapper.UserMapper;
import com.test.pojo.User;

@Service
public class UserServiceImpl implements UserService{

	@Autowired
	private UserMapper userMapper;
	
	public List<User> findAll(){
		
		return userMapper.findAll();
	}
}

持久层

import java.util.List;

import com.test.pojo.User;

public interface UserMapper {

	List<User> findAll();
}

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.test.mapper.UserMapper">

	<select id="findAll" resultType="User">
		SELECT * FROM tb_user
	</select>
	
</mapper>

启动项目,浏览器输入,得到结果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值