架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(二)
2018-03-21 17:12
1211 查看
接下来我们将整合mybatisplus+Oracle,实现一个简单的查询。(期间踩了很多坑,遇到一些问题,还好慢慢解决了。现在是通过配置文件的方式来进行dubbo服务的注册和发布,希望以后能在学习和实践中使用springboot注解方式(也有可能是因为知识还没到那个层面,无法弄懂其中的奥义
))
一、SpringBoot整合mybatisplus
1 众所周知,mybatisplus作为mybatis的一个升级版,大大地简化了大家配置mybatis的xml文件的时间,并且已经整合了很多通用的方法,包括分页的方法等,本项目不细讲mybatisplus,有兴趣的同学可以自己去学一下。本次项目使用mybatisplus作为后台数据库访问的框架。
2 mybatisplus和mybatisplus的SpringBoot依赖:<!--mybatisplus 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<!-- Mybatisplus SpringBoot-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>${mybatisplus-spring-boot-starter.version}</version>
</dependency>
具体的版本根据你的需求来定。
3 对应的实体类UserEntity:@TableName("USER_INFO")
public class UserEntity extends Model<UserEntity> {
@TableId(value = "USER_ID",type = IdType.AUTO)
private Integer userId;
private String username;
private String password;
private Integer age;
protected Serializable pkVal() {
return userId;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "UserEntity{" +
"userId=" + userId +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
public UserEntity(Integer userId, String username, String password, Integer age) {
this.userId = userId;
this.username = username;
this.password = password;
this.age = age;
}
public UserEntity() {
} 4 Mapper层UserMapper:public interface UserMapper extends BaseMapper<UserEntity>{
Integer getCount();
} 5 UserMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.laowang.mapper.UserMapper">
<resultMap id="userMap" type="com.laowang.entity.UserEntity">
<result column="USER_ID" property="userId"></result>
<result column="USERNAME" property="username"></result>
<result column="PASSWORD" property="password"></result>
<result column="AGE" property="age"></result>
</resultMap>
<select id="getCount" resultType="java.lang.Integer">
SELECT COUNT(1) FROM USER_INFO
</select>
</mapper> 6 服务接口层IUserService:public interface IUserService extends IService<UserEntity> {
UserEntity getUserById(int id);
UserEntity findUser();
/**
* 查询总数量
* @return
*/
Integer getCount();
}
7 服务接口实现层UserServiceImpl:@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserEntity> implements IUserService {
@Autowired
private UserMapper userMapper;
public UserEntity getUserById(int id) {
return userMapper.selectById(id);
}
public UserEntity findUser() {
return new UserEntity(20,"laowang","123456789",24);
}
public Integer getCount() {
return userMapper.getCount();
}
} 8 控制层UserController:@RestController
public class UserController {
@Autowired
private IUserService userService;
@GetMapping("/getUser/{id}")
public UserEntity getUser(@PathVariable int id) {
return userService.getUserById(id);
}
@GetMapping("/test")
public UserEntity findUser() {
return userService.findUser();
}
@GetMapping("/getCount")
public Integer getCount() {
return userService.getCount();
}
}9 application.yml:# Dubbo 服务提供者配置
server:
port: 8070
spring:
application:
name: provider
datasource:
url: jdbc:oracle:thin:@127.0.0.1:1521:hadoop
username: sys as sysdba
password: 123456789
driver-class-name: oracle.jdbc.driver.OracleDriver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,logback
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
useGlobalDataSourceStat: true
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
type-aliases-package: com.laowang.entity10 运行项目进行测试,即可得到一个简单的查询结果(由于我的项目已经分割了,所以这里暂时看不到效果,希望大家按照我的步骤去尝试,能够得到一个简答的查询)
))
一、SpringBoot整合mybatisplus
1 众所周知,mybatisplus作为mybatis的一个升级版,大大地简化了大家配置mybatis的xml文件的时间,并且已经整合了很多通用的方法,包括分页的方法等,本项目不细讲mybatisplus,有兴趣的同学可以自己去学一下。本次项目使用mybatisplus作为后台数据库访问的框架。
2 mybatisplus和mybatisplus的SpringBoot依赖:<!--mybatisplus 依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus</artifactId>
<version>${mybatisplus.version}</version>
</dependency>
<!-- Mybatisplus SpringBoot-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatisplus-spring-boot-starter</artifactId>
<version>${mybatisplus-spring-boot-starter.version}</version>
</dependency>
具体的版本根据你的需求来定。
3 对应的实体类UserEntity:@TableName("USER_INFO")
public class UserEntity extends Model<UserEntity> {
@TableId(value = "USER_ID",type = IdType.AUTO)
private Integer userId;
private String username;
private String password;
private Integer age;
protected Serializable pkVal() {
return userId;
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "UserEntity{" +
"userId=" + userId +
", username='" + username + '\'' +
", password='" + password + '\'' +
", age=" + age +
'}';
}
public UserEntity(Integer userId, String username, String password, Integer age) {
this.userId = userId;
this.username = username;
this.password = password;
this.age = age;
}
public UserEntity() {
} 4 Mapper层UserMapper:public interface UserMapper extends BaseMapper<UserEntity>{
Integer getCount();
} 5 UserMapper.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.laowang.mapper.UserMapper">
<resultMap id="userMap" type="com.laowang.entity.UserEntity">
<result column="USER_ID" property="userId"></result>
<result column="USERNAME" property="username"></result>
<result column="PASSWORD" property="password"></result>
<result column="AGE" property="age"></result>
</resultMap>
<select id="getCount" resultType="java.lang.Integer">
SELECT COUNT(1) FROM USER_INFO
</select>
</mapper> 6 服务接口层IUserService:public interface IUserService extends IService<UserEntity> {
UserEntity getUserById(int id);
UserEntity findUser();
/**
* 查询总数量
* @return
*/
Integer getCount();
}
7 服务接口实现层UserServiceImpl:@Service
public class UserServiceImpl extends ServiceImpl<UserMapper,UserEntity> implements IUserService {
@Autowired
private UserMapper userMapper;
public UserEntity getUserById(int id) {
return userMapper.selectById(id);
}
public UserEntity findUser() {
return new UserEntity(20,"laowang","123456789",24);
}
public Integer getCount() {
return userMapper.getCount();
}
} 8 控制层UserController:@RestController
public class UserController {
@Autowired
private IUserService userService;
@GetMapping("/getUser/{id}")
public UserEntity getUser(@PathVariable int id) {
return userService.getUserById(id);
}
@GetMapping("/test")
public UserEntity findUser() {
return userService.findUser();
}
@GetMapping("/getCount")
public Integer getCount() {
return userService.getCount();
}
}9 application.yml:# Dubbo 服务提供者配置
server:
port: 8070
spring:
application:
name: provider
datasource:
url: jdbc:oracle:thin:@127.0.0.1:1521:hadoop
username: sys as sysdba
password: 123456789
driver-class-name: oracle.jdbc.driver.OracleDriver
type: com.alibaba.druid.pool.DruidDataSource
initialSize: 5
minIdle: 5
maxActive: 20
maxWait: 60000
timeBetweenEvictionRunsMillis: 60000
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1 FROM DUAL
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
filters: stat,wall,logback
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
useGlobalDataSourceStat: true
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
#实体扫描,多个package用逗号或者分号分隔
type-aliases-package: com.laowang.entity10 运行项目进行测试,即可得到一个简单的查询结果(由于我的项目已经分割了,所以这里暂时看不到效果,希望大家按照我的步骤去尝试,能够得到一个简答的查询)
相关文章推荐
- 架构实战项目心得(七):使用SpringBoot+Dubbo+Mybatisplus+Oracle搭建后台项目框架(一)
- 【备忘】使用dubbo、spring-boot等技术实现互联网后台服务项目架构视频教程
- 使用spring boot搭建与dubbo整合项目
- SpringBoot+Shiro+MyBatisPlus搭建前后端分离的多模块项目
- springboot-mybatis-plus基本项目框架
- 使用idea+springboot+Mybatis搭建web项目
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
- 【SSH网上商城项目实战03】使用EasyUI搭建后台页面框架
- 【SSH网上商城项目实战03】使用EasyUI搭建后台页面框架
- 使用idea+springboot+Mybatis搭建web项目
- SSM框架——Spring+SpringMVC+Mybatis的搭建教程 一:概述 SSM框架在项目开发中经常使用到,相比于SSH框架,它在仅几年的开发中运用的更加广泛。 Spring作为一个轻量级
- 使用SpringBoot搭建小型项目,集成mybatis,redis,swagger2,并部署在外部容器中。
- spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架
- 使用Spring Boot搭建应用开发框架(一) —— 基础架构(转载)
- 使用myeclipse搭建SpringBoot框架,整合thymeleaf模板和mybatis
- 整合阿里dubbo服务的项目搭建实战(springboot)
- 【SSH网上商城项目实战03】使用EasyUI搭建后台页面框架
- SSM框架——项目详细搭建(Spring+SpringMVC+MyBatis + oracle)
- 架构实战项目心得(九):后台服务工具ldap:统一用户中心ldap工具使用以及安装
- 使用idea+springboot+Mybatis搭建web项目