SpringBoot集成mySql

pom.xml文件中增加如下的依赖:

<!-- 引入mybatis -->
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	 <version>1.3.0</version>
</dependency>
<!-- MySQL 连接驱动依赖 -->
<dependency>
       	<groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.39</version>
 </dependency>

application.properties中设置数据源

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5

数据库表结构:
SpringBoot集成mySql
具体代码如下:
实体类:

package com.example.domain;
public class User {
	public String id;
	public String userName;
	public String company;
	public String age;
	public String sex;
	public String passWord;
	public String getUserName() {
		return userName;
	}
	public void setUserName(String userName) {
		this.userName = userName;
	}
	public String getPassWord() {
		return passWord;
	}
	public void setPassWord(String passWord) {
		this.passWord = passWord;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getCompany() {
		return company;
	}
	public void setCompany(String company) {
		this.company = company;
	}
	public String getAge() {
		return age;
	}
	public void setAge(String age) {
		this.age = age;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
}

dao层

package com.example.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.example.domain.User;
@Mapper
public interface UserMessageMapper {
	 int insert(User uesr);
	 int delete(String id);
	 int updateById(String id);
	 List<User> selectAll();
	 User selectByid(String id);
}

service层

package com.example.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.domain.User;
import com.example.mapper.UserMessageMapper;
@Service
public class UserMessageService {
	@Autowired
	public UserMessageMapper mapper;
	public int  insert(User user){
		return mapper.insert(user);
	}
	public int delete(String id) {
		return mapper.delete(id);
	}
	public int updateById(String id){
		return mapper.updateById(id);
	}
	public List<User> selectAll(){
		return mapper.selectAll();
	}
	public  User  selectByid(String id){
		return mapper.selectByid(id);
	}
}

controller层

package com.example.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.example.domain.User;
import com.example.service.UserMessageService;
@RestController
public class UserMessageController {
	@Autowired
	public UserMessageService service;
	@RequestMapping(value = "/insert" )
	public int  insert(@RequestBody User user){
		  return service.insert(user);
	}
	 @RequestMapping(value="/delete")
	    public int delMessageById(@RequestParam("id") String id){
	            return service.delete(id);
	  }
	 @RequestMapping(value="/selectByid")
	    public User selectByid(@RequestParam("id") String id){
	            return service.selectByid(id);
	  }
}

xml文件

<?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.mapper.UserMessageMapper" >
  <resultMap id="BaseResultMap" type="com.example.domain.User" >
    <id column="id" property="id" jdbcType="VARCHAR" />
    <result column="userName" property="userName" jdbcType="VARCHAR" />
    <result column="passWord" property="passWord" jdbcType="VARCHAR" />
    <result column="age" property="age" jdbcType="VARCHAR" />
    <result column="sex" property="sex" jdbcType="VARCHAR" />
  </resultMap>
  
  <sql id="Base_Column_List" >
    id, userName, passWord,age,sex
  </sql>
  
  
  <insert id="insert" parameterType="com.example.domain.User" >
    insert into user (id, userName, password,age,sex)
    values (#{id,jdbcType=VARCHAR}, #{userName,jdbcType=VARCHAR}, #{passWord,jdbcType=VARCHAR},
      #{age,jdbcType=VARCHAR},#{sex,jdbcType=VARCHAR}
      )
  </insert>
  
  <update id="updateById" parameterType="com.example.domain.User" >
    update user
    set userName = #{userName,jdbcType=VARCHAR},
      age = #{age,jdbcType=TIMESTAMP},
      sex = #{sex,jdbcType=TIMESTAMP},
      passWord = #{passWord,jdbcType=TIMESTAMP}
    where id = #{id,jdbcType=VARCHAR}
  </update>
  
  <delete id="delete" parameterType="java.lang.String" >
    delete from user
    where id = #{id,jdbcType=VARCHAR}
  </delete>

  <select id="selectByid" resultMap="com.example.domain.User">
    select 
          id, userName, password,age,sex
    from user
    where id = #{id,jdbcType=VARCHAR}
  </select>
  <select id="selectAll" resultMap="BaseResultMap">
    select 
          id, userName, password,age,sex
    from user
    order by 
  </select>
</mapper>

 

项目整体结构如图:
SpringBoot集成mySql

通过Application.java类启动项目,浏览器*问http://localhost:8080/selectByid?id=1,结果如下图所示:
SpringBoot集成mySql