```
创建子项目springboot-dubbo-web用于单体测试mybatisplus能否操作mysql
<?xml version="1.0"?>
<project
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.it123</groupId>
<artifactId>springboot-dubbo</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>springboot-dubbo-web</artifactId>
<name>springboot-dubbo-web</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.it123</groupId>
<artifactId>springboot-dubbo-common-code</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.it123</groupId>
<artifactId>springboot-dubbo-common-utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test-autoconfigure</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
application.properties文件内容:
spring.application.name=sdubbo-web
server.port=80
#ָ����ǰ����/Ӧ�õ����֣�ͬ���ķ���������ͬ����Ҫ�ͱ�ķ���ͬ����
dubbo.application.name=sdubbo-web
demo.service.version=1.0.0
dubbo.protocol.name=dubbo
dubbo.protocol.port=20880
#ָ��ע�����ĵ�λ��
dubbo.registry.address=zookeeper://localhost:2181
#ͳһ���÷����ṩ���Ĺ���
sdubbo-web.timeout = 5000
spring.freemarker.suffix=.html
spring.freemarker.request-context-attribute=request
spring.session.store-type=none
spring.datasource.url=jdbc:mysql://localhost:3306/sdubbo?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Users 实体类
package org.it123.sdubbo.web.model;
import com.baomidou.mybatisplus.annotation.TableName;
@TableName(value = "tab_sys_users")
public class Users {
private String name;
private String password;
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;
}
}
UsersService 接口
package org.it123.sdubbo.web.service;
import org.it123.sdubbo.web.model.Users;
import org.springframework.stereotype.Service;
@Service
public interface UsersService {
int add(Users users);
}
UsersServiceImpl 实现类
package org.it123.sdubbo.web.service.impl;
import org.it123.sdubbo.web.dao.UsersDao;
import org.it123.sdubbo.web.model.Users;
import org.it123.sdubbo.web.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UsersServiceImpl implements UsersService {
@Autowired
private UsersDao usersDao;
@Override
public int add(Users users) {
return usersDao.insert(users);
}
}
操作层类
package org.it123.sdubbo.web.dao;
import org.it123.sdubbo.web.model.Users;
import org.springframework.stereotype.Repository;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
@Repository
public interface UsersDao extends BaseMapper<Users> {
}
控制器层
package org.it123.sdubbo.web.controller;
import org.it123.sdubbo.web.model.Users;
import org.it123.sdubbo.web.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UsersController {
@Autowired
private UsersService usersService;
@GetMapping("/demo")
public String demo() {
Users users = new Users();
users.setName("IT123私塾");
users.setPassword("123456");
int mum = usersService.add(users);
if (mum > 0) {
return "增加成功";
} else {
return "增加失败";
}
}
}
入口文件启动类
package org.it123.sdubbo.web;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
@EnableDubbo
@SpringBootApplication
@MapperScan("org.it123.sdubbo.*.dao")
public class webApplication {
public static void main(String[] args) {
SpringApplication.run(webApplication.class, args);
}
}
```