spring boot(一)spring boot+mybatis+mysql搭建ssm项目

1、构建项目

       去spring官网构建spring boot项目

 

spring boot(一)spring boot+mybatis+mysql搭建ssm项目_第1张图片

 

2、添加起步依赖

     

        
			org.springframework.boot
			spring-boot-starter-web
		
		
		
		
		    org.mybatis.spring.boot
		    mybatis-spring-boot-starter
		    1.3.2
		
		
		
		
		    mysql
		    mysql-connector-java
		    8.0.13
        

注意我这用的mysql版本是

如果你用的版本是5.0的,请下载相对应的mysql-connector-java.jar

3、填写配置(官方推荐用yaml文件)所以这里使用yaml进行配置,当然如果你习惯用properties文件配置也可以。

# 服务端口配置
server:
  port: 8888

# 数据源配置
spring:
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/study_data?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    username: devolpment
    password: devolpment

注意url后面的参数配置(useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=GMT%2B8),如果缺少某些参数,可能会出现某些错误,导致连接数据库失败。

同时注意mysql8.0的驱动类变了 :com.mysql.cj.jdbc.Driver(以前是com.mysql.jdbc.Driver)

**  关于spring.datasource的相关配置,可以去这个jar中的org.springframework.boot.autoconfigure.jdbc.DataSourceProperties这个类获取

4、创建数据库

CREATE TABLE `user` (
  `ID` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '主键,用户唯一标识',
  `NAME` varchar(1024) NOT NULL COMMENT '用户名',
  `PWD` varchar(1024) NOT NULL COMMENT '密码',
  `EMIAL` varchar(1024) NOT NULL COMMENT '邮箱地址',
  `BIRTHDAY` date NOT NULL COMMENT '生日',
  `GENDER` char(1) NOT NULL COMMENT '性别',
  PRIMARY KEY (`ID`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
INSERT INTO `USER` VALUES (1,'zhangsan','123456','[email protected]','1995-11-22','男');

 

5、编写实体对象User

package com.chenhui.springbootcrud.entity;

import java.util.Date;

public class User {
	private Long id;
	private String name;
	private String pwd;
	private String email;
	private Date birthday;
	private char gender;
	public User() {
		super();
	}
	public User(Long id, String name, String pwd, String email, Date birthday,
			char gender) {
		super();
		this.id = id;
		this.name = name;
		this.pwd = pwd;
		this.email = email;
		this.birthday = birthday;
		this.gender = gender;
	}
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getPwd() {
		return pwd;
	}
	public void setPwd(String pwd) {
		this.pwd = pwd;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public Date getBirthday() {
		return birthday;
	}
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	public char getGender() {
		return gender;
	}
	public void setGender(char gender) {
		this.gender = gender;
	}
	@Override
	public String toString() {
		return "User [id=" + id + ", name=" + name + ", pwd=" + pwd
				+ ", email=" + email + ", birthday=" + birthday + ", gender="
				+ gender + "]";
	}
	
}

注意数据类型要与mysql表字段对应

6、编写数据访问层UserMapper

package com.chenhui.springbootcrud.dao;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;

import com.chenhui.springbootcrud.entity.User;

@Mapper
public interface UserMapper {
	
	/**
	 * 根据主键获取用户
	 * 
	 * @auth   chenhui
	 * @param id
	 * @return
	 */
	@Results({
		@Result(property = "email", column = "EMIAL")
	}) // 这里如果实体类属性名与字段名不一致,可以采用这种方式
	@Select("SELECT * FROM USER WHERE ID = #{id}")
	public User getOne(@Param("id")Long id);
}

7、编写业务层

package com.chenhui.springbootcrud.service;

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

import com.chenhui.springbootcrud.dao.UserMapper;
import com.chenhui.springbootcrud.entity.User;

@Service
public class UserService {
	@Autowired
	private UserMapper userMapper;
	
	public User getOne(Long id){
		return userMapper.getOne(id);
	}
}

8、启动类配置

package com.chenhui.springbootcrud;

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

@SpringBootApplication
@MapperScan(basePackages = {"com.chenhui.springbootcrud.dao"})//扫描指定包下的@Mapper注解的接口,注册到springbean容器中
public class SpringBootCrudApplication {

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

9、测试

package com.chenhui.springbootcrud;

import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import com.chenhui.springbootcrud.entity.User;
import com.chenhui.springbootcrud.service.UserService;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBootCrudApplicationTests {
	
	@Autowired
	private UserService userService;
	
	@Test
	public void test(){
		User one = userService.getOne(1l);
		assertEquals("zhangsan", one.getName());
	}
	
	

}

结果

 

项目结构

spring boot(一)spring boot+mybatis+mysql搭建ssm项目_第2张图片

项目github地址:https://github.com/RomaDream/spring-boot-crud.git

 

下一篇

如果有什么问题,希望读者能即使提出来!!!

你可能感兴趣的:(spring,boot,java,spring,boot)