简单的,初级springBoot+mybatis整合,手写,亲测。(多多支持)

配置准备:工具idea,数据库mysql

新建项目:

简单的,初级springBoot+mybatis整合,手写,亲测。(多多支持)_第1张图片
创建项目的文件结构以及jdk的版本

简单的,初级springBoot+mybatis整合,手写,亲测。(多多支持)_第2张图片
选择项目所需要的依赖
web选择左侧web
sql选择mysql,jdbc,mybatis
简单的,初级springBoot+mybatis整合,手写,亲测。(多多支持)_第3张图片
修改项目名,finish完成
生成的pom.xml如下



	4.0.0
	
		org.springframework.boot
		spring-boot-starter-parent
		2.1.3.RELEASE
		 
	
	com.gpyh
	boot
	0.0.1-SNAPSHOT
	boot
	Demo project for Spring Boot

	
		1.8
	

	
		
			org.springframework.boot
			spring-boot-starter-data-jpa
		
		
			org.springframework.boot
			spring-boot-starter-jdbc
		
		
			org.springframework.boot
			spring-boot-starter-web
		
		
			org.mybatis.spring.boot
			mybatis-spring-boot-starter
			2.0.0
		

		
			mysql
			mysql-connector-java
			runtime
		
		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	



修改配置文件

本文不使用application.properties文件 而使用更加简洁的application.yml文件。将resource文件夹下原有的application.properties文件删除,创建application.yml配置文件(备注:其实SpringBoot底层会把application.yml文件解析为application.properties),本文创建了两个yml文件(application.yml和application-dev.yml),分别来看一下内容
application.yml 文件

spring:
  profiles:
    active: dev

application-dev.yml 文件

server:
  port: 8080
 
spring:
  datasource:
    username: root
    password: 1234
    url: jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
    driver-class-name: com.mysql.jdbc.Driver
 
mybatis:
  mapper-locations: classpath:mapper/*Mapper.xml
  type-aliases-package: com.example.entity
 
#showSql
logging:
  level:
    com:
      example:
        mapper : debug

两文件解释(特别注意在编写yml文件时,一定注意格式,上下级。在冒号后要有空格):

在项目中配置多套环境的配置方法。
因为现在一个项目有好多环境,开发环境,测试环境,准生产环境,生产环境,每个环境的参数不同,所以我们就可以把每个环境的参数配置到yml文件中,这样在想用哪个环境的时候只需要在主配置文件中将用的配置文件写上就行如application.yml

笔记:在Spring Boot中多环境配置文件名需要满足application-{profile}.yml的格式,其中{profile}对应你的环境标识,比如:

application-dev.yml:开发环境
application-test.yml:测试环境
application-prod.yml:生产环境
至于哪个具体的配置文件会被加载,需要在application.yml文件中通过spring.profiles.active属性来设置,其值对应{profile}值。

注意:启动文件,springboot的启动类不能放在java目录下!!!放在com目录下
否则会报错误:Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.

数据库表的准备

创建user表,自己插入数据

CREATE TABLE `t_user` (
  `id` bigint(10) NOT NULL AUTO_INCREMENT COMMENT '用户id',
  `username` varchar(10) NOT NULL COMMENT '用户名',
  `password` varchar(200) NOT NULL COMMENT '密码',
  `sex` varchar(2) NOT NULL COMMENT '性别',
  `name` varchar(20) DEFAULT NULL COMMENT '姓名',
  `phone` varchar(20) DEFAULT NULL COMMENT '电话',
  `status` int(2) DEFAULT '1' COMMENT '状态0,锁定,1正常',
  `pwd_error_count` int(11) NOT NULL DEFAULT '0' COMMENT '密码错误次数',
  `create_sys_user` bigint(11) DEFAULT NULL COMMENT '创建人id',
  `create_name` varchar(20) DEFAULT NULL COMMENT '创建者姓名',
  `create_time` datetime DEFAULT NULL COMMENT '创建时间',
  `update_sys_user` bigint(20) DEFAULT NULL COMMENT '更新者id',
  `update_name` varchar(20) DEFAULT NULL COMMENT '更新者姓名',
  `update_time` datetime DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`id`),
  UNIQUE KEY `username` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='系统用户表';

项目结构:

分别在resource下创建 mapper文件夹,放 我们的,mapper.xml文件
在main-java-com-example下分别创建:controller,service,dao , entity 文件夹
各文件下放入相应的UserController ,UserService ,UserDao ,User
简单的,初级springBoot+mybatis整合,手写,亲测。(多多支持)_第4张图片
各类代码:controller

package com.example.controller;

import com.example.entity.User;
import com.example.service.UserService;
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;

/**
 * @author zhaozeren
 * @version 1.0
 * @date 2019/3/16
 */
@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("getUser")
    @ResponseBody
    public User getUser(){
        return userService.getUser();
    }
}

service:

package com.example.service;

import com.example.dao.UserDao;
import com.example.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author zhaozeren
 * @version 1.0
 * @date 2019/3/16
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;

    public User getUser() {
        return userDao.selectByPrimaryKey(3L);
    }
}

dao:

package com.example.dao;


import com.example.entity.User;

public interface UserDao {
    int deleteByPrimaryKey(Long id);

    int insert(User record);

    int insertSelective(User record);

    User selectByPrimaryKey(Long id);

    int updateByPrimaryKeySelective(User record);

    int updateByPrimaryKey(User record);
}

entity:

package com.example.entity;

import java.util.Date;

public class User {
    private Long id;

    private String username;

    private String password;

    private String sex;

    private String name;

    private String phone;

    private Integer status;

    private Integer pwdErrorCount;

    private Long createSysUser;

    private String createName;

    private Date createTime;

    private Long updateSysUser;

    private String updateName;

    private Date updateTime;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username == null ? null : username.trim();
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password == null ? null : password.trim();
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex == null ? null : sex.trim();
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name == null ? null : name.trim();
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone == null ? null : phone.trim();
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Integer getPwdErrorCount() {
        return pwdErrorCount;
    }

    public void setPwdErrorCount(Integer pwdErrorCount) {
        this.pwdErrorCount = pwdErrorCount;
    }

    public Long getCreateSysUser() {
        return createSysUser;
    }

    public void setCreateSysUser(Long createSysUser) {
        this.createSysUser = createSysUser;
    }

    public String getCreateName() {
        return createName;
    }

    public void setCreateName(String createName) {
        this.createName = createName == null ? null : createName.trim();
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Long getUpdateSysUser() {
        return updateSysUser;
    }

    public void setUpdateSysUser(Long updateSysUser) {
        this.updateSysUser = updateSysUser;
    }

    public String getUpdateName() {
        return updateName;
    }

    public void setUpdateName(String updateName) {
        this.updateName = updateName == null ? null : updateName.trim();
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }
}

修改启动文件DemoApplication.java

package com.example;

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

@MapperScan("com.example.dao")
@SpringBootApplication
public class DemoApplication {

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

}

启动配置文件,访问 :http://localhost:8080/user/getUser

简单的,初级springBoot+mybatis整合,手写,亲测。(多多支持)_第5张图片

进一步优化结构,将resources下mapper移到 example 目录下:

简单的,初级springBoot+mybatis整合,手写,亲测。(多多支持)_第6张图片
直接访问http://localhost:8080/user/getUser 会报错
简单的,初级springBoot+mybatis整合,手写,亲测。(多多支持)_第7张图片
他说找不找到我们的 dao层的方法即找不到我们的mapper文件

解决:
在我们的pom.xml中的build下添加(让其可以解析到classpath下的xml)


			
				src/main/java
				
					**/*.xml
				
				false
			
			
				src/main/resources
				
					*
				
				false
			
		

修改application-dev.yml中的扫描路径:

mybatis:
  mapper-locations: classpath*:**/*Mapper.xml
  type-aliases-package: com.example.entity

简单的,初级springBoot+mybatis整合,手写,亲测。(多多支持)_第8张图片
再次访问http://localhost:8080/user/getUser
简单的,初级springBoot+mybatis整合,手写,亲测。(多多支持)_第9张图片
对于启动后的图案修改,可在resources下创建banner.txt将自己想生成的字符图案放入里面即可。推荐字符转字母图工具:生成字母图工具
简单的,初级springBoot+mybatis整合,手写,亲测。(多多支持)_第10张图片
第一次编写博客希望大家多多支持,如果对你有帮助点个赞。下一篇结合当前项目进一步升级。boot天生不支持jsp,但是我们通过配置
boot整合jsp,返回jsp页面.

你可能感兴趣的:(springBoot)