Springboot项目搭建(1)-创建,整合mysql/oracle,druid配置,简单的CRUD
源码地址:https://github.com/VioletSY/article-base
1:创建一个基本项目:https://www.cnblogs.com/excellencesy/p/12522248.html
2:整合oracle:
(1)添加依赖(pom.xml):
<!--jdbc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/com.oracle/ojdbc6 -->
<dependency>
<groupId>com.jslsolucoes</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
(2)数据库信息配置(application.properties):
spring.datasource.url=jdbc:oracle:thin:@127.0.0.1:1521:shitan
spring.datasource.username=xzzc
spring.datasource.password=xzzc
spring.datasource.driver-class-name=oracle.jdbc.driver.OracleDriver
3,整合MySQL
(1)添加依赖(pom.xml):
<!-- mysql驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
(2)数据库信息配置(application.properties):
#MySQL数据源
spring.datasource.url=jdbc:mysql://localhost:3306/base?useUnicode=true&characterEncoding=utf8
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=123456
注:我写的事单数据源,在测试完连接Oracle的功能后就把oracle的配置注释了
4,数据库连接池配置
(1)添加依赖(pom.xml):
<!-- 数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.6</version>
</dependency>
(2)添加mybatis依赖
<!-- Spring-Mybatis -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
(3)添加配置类(DBConfig.java):
@MapperScan(basePackages = "com.googosoft.dao"):将com.googosoft.dao包中的接口/类加上@Mapper注解,从而将该包下的接口/类是注入到spring容器中。
dataSource() :该方法通过@Bean,将其返回值new DruidDataSource()注入到spring容器中。
@Configuration
@MapperScan(basePackages = "com.googosoft.dao")
public class DBConfig { @ConfigurationProperties(prefix = "spring.datasource")
@Bean(initMethod = "init", destroyMethod = "close", name = "dataSource")
public DruidDataSource dataSource() {
return new DruidDataSource();
}
}
5,写一个简单的CRUD
(1)准备一个数据库,新建user表:

(2)pojo:
package com.googosoft.pojo;
public class User {
private String id;
private String name;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
(3)dao层
package com.googosoft.dao.user; import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.googosoft.pojo.User; public interface UserDao { @Insert("INSERT INTO USER ( id, NAME ) VALUES ( #{id}, #{name} )")
public int addUser(User user); @Delete("DELETE FROM USER WHERE id = #{id}")
public int deleteUser(@Param("id")String id); @Select("select id,name from user")
public List<User> getUserList(); @Update("update user set name=#{name} where id=#{id}")
public int updateUser(@Param("id")String id,@Param("name")String name); }
(4)service层
package com.googosoft.service.user; import java.util.List; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import com.googosoft.dao.user.UserDao;
import com.googosoft.pojo.User; @Service
public class UserService { @Autowired
private UserDao userDao; public int addUser(User user) {
return userDao.addUser(user);
} public int deleteUser(String id) {
return userDao.deleteUser(id);
} public List<User> getUserList() {
return userDao.getUserList();
} public int updateUser(String id,String name) {
return userDao.updateUser(id,name);
}
}
(5)controller层
package com.googosoft.controller.user; import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.googosoft.pojo.User;
import com.googosoft.service.user.UserService; @RestController
@RequestMapping("user")
public class UserController {
@Autowired
UserService service; @RequestMapping("test")
public String test(String id,String name){
return "test";
} @RequestMapping("addUser")
public int addUser(String id,String name){
User user = new User();
user.setId(id);
user.setName(name);
return service.addUser(user);
} @RequestMapping("deleteUser")
public int deleteUser(String id){
return service.deleteUser(id);
} @RequestMapping("getUserList")
public List<User> getUserList(){
return service.getUserList();
} @RequestMapping("updateUser")
public int updateUser(String id,String name){
return service.updateUser(id,name);
}
}
(6)简单的测试
(1)查列表:http://127.0.0.1:8089/user/getUserList

(2)添加:http://127.0.0.1:8089/user/addUser?id=002&name=lisi

(3)更新:http://127.0.0.1:8089/user/updateUser?id=002&name=zhangsan

(4)删除:http://127.0.0.1:8089/user/deleteUser?id=002

Springboot项目搭建(1)-创建,整合mysql/oracle,druid配置,简单的CRUD的更多相关文章
- 网站后台搭建--springboot项目是如何创建的
在创建项目之前先说一下ide的问题,从学习软件开始一直到一个月之前,开发用的IDE都是Eclipse,对,就是这个远古时代的开发工具,在使用过程中虽然总是遇到各种bug,但内心里还是存在着一丝理解的想 ...
- SpringBoot 项目搭建(详细介绍+案例源码)
SpringBoot 项目搭建 SpringBoot 项目整合源码 SpringBoot 项目整合 一.项目准备 1.1 快速创建 SpringBoot 项目 1.2 标准项目结构图如下 1.3 添加 ...
- SpringBoot项目搭建 + Jwt登录
临时接了一个小项目,有需要搭一个小项目,简单记录一下项目搭建过程以及整合登录功能. 1.首先拿到的是一个码云地址,里面是一个空的文件夹,只有一个 2. 拿到HTTPS码云项目地址链接,在IDEA中cl ...
- SpringBoot之入门教程-SpringBoot项目搭建
SpringBoot大大的简化了Spring的配置,把Spring从配置炼狱中解救出来了,以前天天配置Spring和Mybatis,Springmvc,Hibernate等整合在一起,感觉用起来还是挺 ...
- spring原理案例-基本项目搭建 03 创建工程运行测试 spring ioc原理实例示例
下面开始项目的搭建 使用 Java EE - Eclipse 新建一 Dynamic Web Project Target Runtime 选 Apache Tomcat 7.0(不要选 Apache ...
- 【转载】JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案
JAVA SpringBoot 项目打成jar包供第三方引用自动配置(Spring发现)解决方案 本文为转载,原文地址为:https://www.cnblogs.com/adversary/p/103 ...
- SpringBoot项目构建、测试、热部署、配置原理、执行流程
SpringBoot项目构建.测试.热部署.配置原理.执行流程 一.项目构建 二.测试和热部署 三.配置原理 四.执行流程
- idea:spring initializr无web勾选,maven方式搭建springboot项目。jdk7创建springboot项目的版本不兼容问题。
一.idea 使用spring initializr不选择web搭建springboot项目 1.file => new => project 2.直接next到finish结束. 3.完 ...
- springboot系列二、springboot项目搭建
一.官网快速构建 1.maven构建项目 1.访问http://start.spring.io/ 2.选择构建工具Maven Project.Spring Boot版本2.1.1以及一些工程基本信息, ...
随机推荐
- mysql cmd链接不上数据库情况汇总
在我的电脑 属性 高级设置 环境变量 path 编辑 添加mysql bin的文件位置复制粘贴上 mysql> use mysqlERROR 1044 (42000): Access denie ...
- C++ 跨dll传递string类型参数执行出错问题
今天遇到一个问题,在一个dll工程中定义了一个返回值为string,参数为string的函数,然后在一个测试工程中调用,Release模式下一切正常Debug模式下整个函数的执行到return之前都毫 ...
- vga显示原理即相关计算
行扫描周期:完成一行扫描所需时间: 行时序时间(a,b,c,d,e):完成一个像素点显示得时间 场扫描周期:完成所有行(一帧扫描所需时间) 场时序时间(o,p,q,r,s):完成一行显示得时间,一个完 ...
- 解决win10状态栏的搜索框无法搜索本地应用或无反应
今天突然出现的问题,在状态栏左下角的搜索框搜索OneNote没有任何反应. 1.首先,打开管理员命令窗口,win+x,可以看到弹出一个窗口,打开windows Powershell(管理员)如图 2, ...
- JFrog推出全球首个支持混合云架构,端到端的通用DevOps平台 ——JFrog Platform
JFrog Platform,基于屡获殊荣的JFrog Artifactory制品仓库的独特能力,通过多合一的体验提供DevSecOps.CI / CD和软件分发的解决方案. 2020 ...
- 苹果公司以注重客户隐私闻名世界,但为什么Siri泄露了我的秘密?
编辑 | 于斌 出品 | 于见(mpyujian) 苹果的Siri因为其作为智能语音助手,方便人们打电话.发信息等功能,被人们所喜爱,但是最近,Siri好像有一些问题,让我们怀疑这位"小伙伴 ...
- 在同一个tomcat下部署多个springboot项目时,springboot项目无法正常启动的问题
这个问题是基于,不使用springboot内置的tomcat会产生(即使用自己的tomcat时). 今天在部署springboot项目的时候遇到了一个问题,怎么部署都访问不了,在网上查了很多原因,什么 ...
- jquery grid 获取选中的行的数据,以及获取所有行的方法
https://blog.csdn.net/shenqingkeji/article/details/52861319
- js实现文字上下滚动
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 闲来无事.gif