大家都知道,现在Spring框架几乎无处不用,目前最新版本据说是Spring5,现在常用的基本还是Spring4.X,很多公司甚至用的还是Spring3.x,而Spring领域其中最好用的莫过于SpringBoot,这是从SpringMVC衍变出来的,本质还是SpringMVC,但是要比SpringMVC好用的多,因为很好用,所以笔者闲暇之际用SpringBoot从头至尾摸索着搭建了一个案例,分享出来,希望对感兴趣的朋友有所帮助。废话不说了,进入主题,直接分享案例搭建全流程。
本项目使用的环境:
额外功能
步骤:
4.0.0
com.ocean
springboot-mybatis-demo
0.0.1-SNAPSHOT
jar
springboot-mybatis-demo
Demo project for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.10.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-jdbc
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.1
org.springframework.boot
spring-boot-starter-web
mysql
mysql-connector-java
5.1.6
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-maven-plugin
org.mybatis.generator
mybatis-generator-maven-plugin
1.3.1
${basedir}/src/main/resources/generator/generatorConfig.xml
true
true
server:
port: 8080
spring:
datasource:
name: test
url: jdbc:mysql://127.0.0.1:3306/chenyun01
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapping/*Mapper.xml
type-aliases-package: com.ocean.pojo
package com.ocean.dao;
import com.ocean.pojo.UserInfo;
import java.util.List;
public interface UserInfoMapper {
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userinfo
*
* @mbggenerated
*/
int insert(UserInfo record);
/**
* This method was generated by MyBatis Generator.
* This method corresponds to the database table userInfo
*
* @mbggenerated
*/
int insertSelective(com.ocean.pojo.UserInfo record);
//这个方式我自己单独加的方法
List findAllUser();
}
package com.ocean.pojo;
public class UserInfo {
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column userinfo.userId
*
* @mbggenerated
*/
private Integer userid;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column userinfo.userName
*
* @mbggenerated
*/
private String username;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column userinfo.phone
*
* @mbggenerated
*/
private String phone;
/**
* This field was generated by MyBatis Generator.
* This field corresponds to the database column userinfo.sex
*
* @mbggenerated
*/
private String sex;
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column userinfo.userId
*
* @return the value of userinfo.userId
*
* @mbggenerated
*/
public Integer getUserid() {
return userid;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column userinfo.userId
*
* @param userid the value for userinfo.userId
*
* @mbggenerated
*/
public void setUserid(Integer userid) {
this.userid = userid;
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column userinfo.userName
*
* @return the value of userinfo.userName
*
* @mbggenerated
*/
public String getUsername() {
return username;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column userinfo.userName
*
* @param username the value for userinfo.userName
*
* @mbggenerated
*/
public void setUsername(String username) {
this.username = username == null ? null : username.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column userinfo.phone
*
* @return the value of userinfo.phone
*
* @mbggenerated
*/
public String getPhone() {
return phone;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column userinfo.phone
*
* @param phone the value for userinfo.phone
*
* @mbggenerated
*/
public void setPhone(String phone) {
this.phone = phone == null ? null : phone.trim();
}
/**
* This method was generated by MyBatis Generator.
* This method returns the value of the database column userinfo.sex
*
* @return the value of userinfo.sex
*
* @mbggenerated
*/
public String getSex() {
return sex;
}
/**
* This method was generated by MyBatis Generator.
* This method sets the value of the database column userinfo.sex
*
* @param sex the value for userinfo.sex
*
* @mbggenerated
*/
public void setSex(String sex) {
this.sex = sex == null ? null : sex.trim();
}
}
对于映射文件UserInfoMapper.xml,删除多余的“main.java.”字符,配置xml的namespace
insert into userinfo (userId, userName, phone,
sex)
values (#{userid,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR})
insert into userinfo
userId,
userName,
phone,
sex,
#{userid,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{sex,jdbcType=VARCHAR},
package com.ocean.springbootmybatisdemo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.web.servlet.ErrorPage;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.http.HttpStatus;
@SpringBootApplication
@MapperScan("com.ocean.dao")
@ComponentScan(basePackages = {"com.ocean.*"})
public class SpringbootMybatisDemoApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisDemoApplication.class, args);
}
}
12.到这里本案例的搭建工作都完成了,接下来就是直接在浏览器上测试: 现在把controller,service层的代码都写好(因做演示,此处控制器只写了一个方法):UserController.java
package com.ocean.controller;
import com.ocean.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping(value = {"/user"})
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping(value = {"/findAll"},produces = {"application/json;charset=UTF-8"},method = RequestMethod.GET)
public List getAllUsers(){
List list = userService.findAllUser();
return list;
}
}
package com.ocean.service;
import java.util.List;
public interface IUserService {
List findAllUser();
}
UserServiceImpl.java
package com.ocean.service.impl;
import com.ocean.dao.UserInfoMapper;
import com.ocean.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service(value = "userService")
public class UserServiceImpl implements IUserService {
@Autowired
private UserInfoMapper userDao;//这里可能会报错,但是并不会影响
@Override
public List findAllUser(){
return userDao.findAllUser();
}
}
如果强迫症看不下去上面那个报错:(解决方法)源码地址:https://github.com/guobinhui/javaWeb-project/tree/master/springboot-mybatis-demo
更多JavaEE资料请关注下面公众号,欢迎广大开发者朋友一起交流。笔者电话(微信):18629374628