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

17 篇文章 0 订阅
7 篇文章 0 订阅

1、构建项目

       去spring官网构建spring boot项目

 

 

2、添加起步依赖

     

        <dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		
		<!-- spring-boot与mybatis集成 -->
		<dependency>
		    <groupId>org.mybatis.spring.boot</groupId>
		    <artifactId>mybatis-spring-boot-starter</artifactId>
		    <version>1.3.2</version>
		</dependency>
		
		<!-- mysql引擎 -->
		<dependency>
		    <groupId>mysql</groupId>
		    <artifactId>mysql-connector-java</artifactId>
		    <version>8.0.13</version>
        </dependency>

注意我这用的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','947840093@qq.com','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());
	}
	
	

}

结果

 

项目结构

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

 

下一篇

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

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值