Java开发千人交流群 677807540 大家可以一起讨论技术欢迎加入
3.第一次新建spring boot项目时,可能没有这些选项,需要安装spring boot 相关的插件,选择工具条上的help–>Eclipse marketplace后选择popular,安装sts重启eclipse后就可以新建工程了
4.点击之后进去选择进行安装
5.选择spring starter project然后点击next
6.将jdbc、orzcle、web、mybatis 选上 点击finish这样 一个spring boot项目就新建好了。
7.新建项目有可能pom.xml会报错,按照我给的应该不会报错,代码如下。
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.2.0.RELEASE
com.example
springboot
0.0.1-SNAPSHOT
springboot
Demo project for Spring Boot
1.8
3.1.1
org.springframework.boot
spring-boot-starter-jdbc
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.1
com.oracle.ojdbc
ojdbc8
runtime
org.springframework.boot
spring-boot-starter-test
test
org.junit.vintage
junit-vintage-engine
org.springframework.boot
spring-boot-starter-thymeleaf
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
8.application.properties里面的配置,有关数据库,日志打印(sql日志打印需要该自己项目的路径)
# Springboot \u914D\u7F6E
server.port=8080
spring.servlet.multipart.maxFileSize=300MB
spring.servlet.multipart.maxRequestSize=300MB
server.tomcat.uri-encoding=UTF-8
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
spring.http.encoding.force=true
spring.messages.encoding=UTF-8
spring.datasource.druid.web-stat-filter.enabled=true
spring.datasource.druid.test-while-idle=true
spring.datasource.druid.test-on-borrow=false
spring.datasource.druid.min-idle=2
spring.datasource.druid.max-active=10
spring.datasource.url=jdbc:oracle:thin:@数据库地址
spring.datasource.username=用户名
spring.datasource.password=密码
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
spring.datasource.max-idle=10
spring.datasource.max-wait=10000
spring.datasource.min-idle=5
spring.datasource.initial-size=5
# mybatis
mybatis.configuration.map-underscore-to-camel-case=true
# actuator \u914D\u7F6E
management.endpoint.health.show-details=always
logging.level.root=INFO
#添加sql日志打印
logging.level.com.example.demo.mapper: debug
9.创建Controller ,service,mapper,model等类进行测试springboot
10.controller代码
package com.example.demo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.demo.model.ComUser;
import com.example.demo.service.ComUserService;
@RequestMapping("/comUser")
@RestController
public class ComUserController {
@Autowired
private ComUserService comUserService;
@GetMapping("/insert")
public void insert() {
ComUser ComUser = new ComUser();
ComUser.setUesrName("springboot");
ComUser.setUserId("1111");
comUserService.insertComUser(ComUser);
}
}
11.mapper代码
package com.example.demo.mapper;
import org.apache.ibatis.annotations.Mapper;
import com.example.demo.model.ComUser;
@Mapper
public interface ComUserMapper {
public void insertComUser(ComUser comUser);
}
12.mapper.xml代码
insert into COM_USER (USER_ID, USER_CODE, UESR_NAME,
PASSWORD, USER_CLASS_ID, IDCARD,
BLACK_PIC)
values (#{userId,jdbcType=VARCHAR}, #{userCode,jdbcType=VARCHAR}, #{uesrName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR}, #{userClassId,jdbcType=VARCHAR}, #{idcard,jdbcType=VARCHAR},
#{blackPic,jdbcType=VARCHAR})
insert into COM_USER
USER_ID,
USER_CODE,
UESR_NAME,
PASSWORD,
USER_CLASS_ID,
IDCARD,
BLACK_PIC,
#{userId,jdbcType=VARCHAR},
#{userCode,jdbcType=VARCHAR},
#{uesrName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{userClassId,jdbcType=VARCHAR},
#{idcard,jdbcType=VARCHAR},
#{blackPic,jdbcType=VARCHAR},
13.model实体类代码
package com.example.demo.model;
public class ComUser {
/**
* 主键id
*/
private String userId;
/**
* 用户账号
*/
private String userCode;
/**
* 用户姓名
*/
private String uesrName;
/**
* 用户密码
*/
private String password;
/**
* null
*/
private String userClassId;
/**
* null
*/
private String idcard;
/**
* null
*/
private String blackPic;
/**
* 主键id
* @return USER_ID 主键id
*/
public String getUserId() {
return userId;
}
/**
* 主键id
* @param userId 主键id
*/
public void setUserId(String userId) {
this.userId = userId == null ? null : userId.trim();
}
/**
* 用户账号
* @return USER_CODE 用户账号
*/
public String getUserCode() {
return userCode;
}
/**
* 用户账号
* @param userCode 用户账号
*/
public void setUserCode(String userCode) {
this.userCode = userCode == null ? null : userCode.trim();
}
/**
* 用户姓名
* @return UESR_NAME 用户姓名
*/
public String getUesrName() {
return uesrName;
}
/**
* 用户姓名
* @param uesrName 用户姓名
*/
public void setUesrName(String uesrName) {
this.uesrName = uesrName == null ? null : uesrName.trim();
}
/**
* 用户密码
* @return PASSWORD 用户密码
*/
public String getPassword() {
return password;
}
/**
* 用户密码
* @param password 用户密码
*/
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
/**
* null
* @return USER_CLASS_ID null
*/
public String getUserClassId() {
return userClassId;
}
/**
* null
* @param userClassId null
*/
public void setUserClassId(String userClassId) {
this.userClassId = userClassId == null ? null : userClassId.trim();
}
/**
* null
* @return IDCARD null
*/
public String getIdcard() {
return idcard;
}
/**
* null
* @param idcard null
*/
public void setIdcard(String idcard) {
this.idcard = idcard == null ? null : idcard.trim();
}
/**
* null
* @return BLACK_PIC null
*/
public String getBlackPic() {
return blackPic;
}
/**
* null
* @param blackPic null
*/
public void setBlackPic(String blackPic) {
this.blackPic = blackPic == null ? null : blackPic.trim();
}
}
14.service以及serviceimpl方法
package com.example.demo.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.model.ComUser;
@Service
public interface ComUserService {
public void insertComUser(ComUser comUser);
}
package com.example.demo.service.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.demo.mapper.ComUserMapper;
import com.example.demo.model.ComUser;
import com.example.demo.service.ComUserService;
@Service
public class ComUserServiceImpl implements ComUserService {
@Autowired
private ComUserMapper comUserMapper;
@Override
public void insertComUser(ComUser comUser) {
// TODO Auto-generated method stub
comUserMapper.insertComUser(comUser);
}
}
15.启动类代码,这里需要添加自己mapper路径进行扫描,不然会报错
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.demo.mapper")
public class SpringbootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootApplication.class, args);
}
}
16.启动springboot
16.启动成功
17.访问成功
18.数据插入成功