SpringBoot学习七:springboot整合mybatis详解

springboot对mybatis有很好的支持

整合目录如下:


下面是整合逻辑:

1、配置pom.xml

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

	<groupId>com.springboot01</groupId>
	<artifactId>demo</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>

	<name>springboot01</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.3.3.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.7</java.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>


		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-freemarker</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-jdbc</artifactId>
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version>
		</dependency>
		
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.21</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>
		
	</dependencies>
	

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

2、配置application.properties

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

3、新建数据库hi,表名为t_user,字段为id,name.

4、新建User实体类

package com.sm.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;

@Entity(name="t_user")
public class User {

	@Id
	@GeneratedValue
	private Integer id;
	@Column
	private String name;
	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;
	}
	
	
}

5、新建UserMapper.java

package com.sm.mapper;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.context.annotation.Configuration;

import com.sm.entity.User;

@Configuration
public interface UserMapper {

	@Select("SELECT * FROM t_user WHERE NAME = #{name}")
	User findByName(@Param("name") String name);
	
	@Insert("INSERT INTO t_user(NAME) VALUES(#{name})")
	int insert(@Param("name") String name);

}

6、新建UserService.java

package com.sm.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.sm.dao.UserDao;
import com.sm.entity.User;
import com.sm.mapper.UserMapper;

@Service
public class UserService {

	
	@Autowired
	private UserMapper userMapper;
	
	public void createUser(String name) {
		System.out.println("ssss");
		userMapper.insert(name);
		System.out.println("创建数据成功");
	}

	
	public User getUser(String name) {
		System.out.println("dddd");
		User user = userMapper.findByName(name);
		System.out.println("查询成功");
		return user;
	}
	
	
}

7、新建UserController.java

package com.sm.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.sm.entity.User;
import com.sm.service.UserService;

@RestController
public class UserController {

	@Resource
	private UserService userService;

	
	@RequestMapping("/createUser")
	public String createUser(String userName) {
		userService.createUser(userName);
		return "success";
		
	}
	
	@RequestMapping("/getUser")
	public String getUser(String name) {
		User user = userService.getUser(name);
		System.out.println(user.getName());
		return "success";
		
	}
	
}

新建App.java,作为启动类

package com.sm.controller;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@ComponentScan(basePackages = "com.sm")
@MapperScan(basePackages = "com.sm.mapper")
@SpringBootApplication
public class App {

	public static void main(String[] args) {
		SpringApplication.run(App.class, args);
	}
}

这样,springboot整合mybatis就初步搭建成功了

启动项目。


查询成功

猜你喜欢

转载自blog.csdn.net/cuipeng1019/article/details/80302414