Eclipse 搭建 SpringBoot 2.0 项目 (四) 使用 Mybatis 访问MySQL数据库

SpringBoot 入门系列:

Eclipse 搭建 SpringBoot 2.0 项目 (一) 开发环境的搭建(安装开发工具STS)

Eclipse 搭建 SpringBoot 2.0 项目 (二) 简单项目的搭建

Eclipse 搭建 SpringBoot 2.0 项目 (三) 使用 JPA 访问MySQL数据库

Eclipse 搭建 SpringBoot 2.0 项目 (四) 使用 Mybatis 访问MySQL数据库

 

在上一篇文章中讲述了通过JPA访问MySQL,这一篇讲述Mybatis访问数据库

1、使用 STS 新建一个名为demo-jpa的项目,选择MyBatis,MySQL以及Web

创建完成后,打开 application.properties,设置数据库及MyBatis的相关属性

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=

#mybatis.config-location=classpath:mybatis-config.xml    mybatis mapper文件的位置
mybatis.mapper-locations=classpath*:mapper/**/*.xml
#扫描pojo类的位置,在此处指明扫描实体类的包,在mapper中就可以不用写pojo类的全路径名了
mybatis.type-aliases-package=com.example.demo

2、创建 User.java 、UserDao.java 、MainController.java。如下:

Eclipse 搭建 SpringBoot 2.0 项目 (四) 使用 Mybatis 访问MySQL数据库

package com.example.demo;
public class User {
    private Integer id;
    private String name;
    private String email;
	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 String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
}
package com.example.demo;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserDao {
        //根据姓名有条件查询
	User selectUserByName(@Param("name")String name)throws Exception;
        //插入新数据
	User insertUser(@Param("id")String id,@Param("name")String
        name,@Param("email")String email)throws Exception;
        //查询全部数据
	List<User> findAll()throws Exception;
}

 

package com.example.demo;

import java.util.List;
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;

@Controller 
public class MainController {	
	@Autowired	
	UserDao userDao;	
	@RequestMapping(value = "/hello")
	public @ResponseBody String hello(){
		System.out.println("Hello");
		return "hello mybatis";
	}	
	@RequestMapping(value = "/findByName")
	public @ResponseBody User findByName(String name) throws Exception{
		User user = userDao.selectUserByName(name);
		System.out.println(user);
		return  user;
	}
	@RequestMapping(value = "/all")
	public @ResponseBody List<User> all() throws Exception{
		List<User> user = userDao.findAll();
		System.out.println(user);
		return  user;
	}
	@RequestMapping(value = "/insert")
	public @ResponseBody String insertUser(String id,String name,String email) throws
        Exception{
		userDao.insertUser(id,name,email);	
		System.out.println(id+name+email);
		return  "saved";
	}
}

3、创建mapper文件夹,新建UserDaoMapper.xml

Eclipse 搭建 SpringBoot 2.0 项目 (四) 使用 Mybatis 访问MySQL数据库

<?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.example.demo.UserDao">

<!-->根据名字有条件查询  <-->
<select id="selectUserByName" resultType="com.example.demo.User" parameterType="string">
		select * from user where name = #{name}
</select>

<!-->查询全部  <-->
<select id="findAll" resultType="com.example.demo.User" parameterType="string">
		select * from user 
</select>

<!-->插入新数据  <-->
<select id="insertUser" resultType="com.example.demo.User" parameterType="string">
		insert into user(id,name,email) value (#{id} , #{name} , #{email})
</select>
</mapper>

 4、启动项目,测试

查询所有数据: http://localhost:8080/all

Eclipse 搭建 SpringBoot 2.0 项目 (四) 使用 Mybatis 访问MySQL数据库

插入新数据:  http://localhost:8080/insert?id=3&name=henry&[email protected]

Eclipse 搭建 SpringBoot 2.0 项目 (四) 使用 Mybatis 访问MySQL数据库

根据名字查询:  http://localhost:8080/findByName?name=henry

Eclipse 搭建 SpringBoot 2.0 项目 (四) 使用 Mybatis 访问MySQL数据库

以上,就是用MyBatis访问MySQL的案例。附上参考代码:

https://pan.baidu.com/s/16t9eGqQ_sK_5HRDsF6O99g