源码地址: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