文章目录
SpringBoot集成dubbo,Mybatis,Spring,SpringMVC,JSP
a.接口工程:存放实体bean和业务接口
b.服务提供者:它是一个SpringBoot框架web项目,集成MyBatis,Redis
c.服务消费者:它是一个springboot框架web项目,集成JSP,dubbo
a.接口工程
Student
package com.utkvrjan.springboot.model;
import java.io.Serializable;
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
StduentService
package com.utkvrjan.springboot.service;
import com.utkvrjan.springboot.model.Student;
public interface StudentService {
Student queryStudentById(Integer id);
Integer queryAllStudentCount();
}
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.utkvrjan.springboot</groupId>
<artifactId>024-springboot-dubbo-ssm-interface</artifactId>
<version>1.0.0</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
</project>
b.服务提供者
StudentMapper.java
package com.utkvrjan.springboot.mapper;
import com.utkvrjan.springboot.model.Student;
public interface StudentMapper {
int deleteByPrimaryKey(Integer id);
int insert(Student record);
int insertSelective(Student record);
Student selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Student record);
int updateByPrimaryKey(Student record);
Integer selectAllStudentCount();
}
StudentMapper.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.utkvrjan.springboot.mapper.StudentMapper">
<resultMap id="BaseResultMap" type="com.utkvrjan.springboot.model.Student">
<id column="id" jdbcType="INTEGER" property="id" />
<result column="name" jdbcType="VARCHAR" property="name" />
<result column="age" jdbcType="INTEGER" property="age" />
</resultMap>
<sql id="Base_Column_List">
id, name, age
</sql>
<select id="selectByPrimaryKey" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List" />
from t_student
where id = #{id,jdbcType=INTEGER}
</select>
<delete id="deleteByPrimaryKey" parameterType="java.lang.Integer">
delete from t_student
where id = #{id,jdbcType=INTEGER}
</delete>
<insert id="insert" parameterType="com.utkvrjan.springboot.model.Student">
insert into t_student (id, name, age
)
values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER}
)
</insert>
<insert id="insertSelective" parameterType="com.utkvrjan.springboot.model.Student">
insert into t_student
<trim prefix="(" suffix=")" suffixOverrides=",">
<if test="id != null">
id,
</if>
<if test="name != null">
name,
</if>
<if test="age != null">
age,
</if>
</trim>
<trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="id != null">
#{id,jdbcType=INTEGER},
</if>
<if test="name != null">
#{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
#{age,jdbcType=INTEGER},
</if>
</trim>
</insert>
<update id="updateByPrimaryKeySelective" parameterType="com.utkvrjan.springboot.model.Student">
update t_student
<set>
<if test="name != null">
name = #{name,jdbcType=VARCHAR},
</if>
<if test="age != null">
age = #{age,jdbcType=INTEGER},
</if>
</set>
where id = #{id,jdbcType=INTEGER}
</update>
<update id="updateByPrimaryKey" parameterType="com.utkvrjan.springboot.model.Student">
update t_student
set name = #{name,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
</update>
<!--获取学生总人数-->
<select id="selectAllStudentCount" resultType="java.lang.Integer">
select count(*) from t_student
</select>
</mapper>
StudentServiceImpl
package com.utkvrjan.springboot.service;
import com.alibaba.dubbo.config.annotation.Service;
import com.utkvrjan.springboot.mapper.StudentMapper;
import com.utkvrjan.springboot.model.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.Redistemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
@Component
@Service(interfaceName = "com.utkvrjan.springboot.service.StudentService",version="1.0.0",timeout = 15000) //用dubbo的service
public class StudentServiceImpl implements StudentService {
@Autowired
private StudentMapper studentMapper;
@Autowired
private Redistemplate<Object,Object> redistemplate;
@Override
public Student queryStudentById(Integer id) {
return studentMapper.selectByPrimaryKey(id);
}
@Override
public Integer queryAllStudentCount() {
//提升系统性能,用户体验提升
//首先从redis缓存中查询,如果有:直接使用,如果没有:去数据库查询并存放到redis缓存中
Integer allStudentCount=(Integer)redistemplate.opsForValue().get("allStudentCount");
if(null==allStudentCount){
//去数据库查询
allStudentCount=studentMapper.selectAllStudentCount();
//并存放到redis缓存中
//redis如果你不去设置有效期的话,它是永久不失效,所以后两个参数就是设置有效期,设置成了15秒
redistemplate.opsForValue().set("allStudentCount",allStudentCount,15, TimeUnit.MINUTES);
}
return allStudentCount;
}
}
Application
package com.utkvrjan.springboot;
import com.alibaba.dubbo.spring.boot.annotation.EnabledubboConfiguration;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan(basePackages = "com.utkvrjan.springboot.mapper")
@EnabledubboConfiguration //开启dubbo配置
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties
# 设置内嵌Tomcat
server.port=8081
server.servlet.context-path=/
#设置连接数据库信息
spring.datasource.driver-class-name=com.MysqL.cj.jdbc.Driver
spring.datasource.username=root
spring.datasource.url=jdbc:MysqL://localhost:3306/mydb1?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneshift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8
spring.datasource.password=1234
#设置dubbo设置
spring.application.name=025-springboot-dubbo-ssm-provider
#声明当前工程为服务提供者(在idea中,此文件下编写dubbo相关内容就是没有自动提示,不要因为这个担心自己是不是写错)
spring.dubbo.server=true
#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181
# 设置redis配置
spring.redis.host=localhost
spring.redis.port=6379
# spring.redis.password=1234 没有设置密码就不用写
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.utkvrjan.springboot</groupId>
<artifactId>025-springboot-dubbo-ssm-provider</artifactId>
<version>1.0.0</version>
<name>025-springboot-dubbo-ssm-provider</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--接口工程-->
<dependency>
<groupId>com.utkvrjan.springboot</groupId>
<artifactId>024-springboot-dubbo-ssm-interface</artifactId>
<version>1.0.0</version>
</dependency>
<!--dubbo依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--注册中心-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--MyBatis集成springboot起步依赖-->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<!--MysqL驱动-->
<dependency>
<groupId>MysqL</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--springboot集成REdis起步依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
<build>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
GeneratorMapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!-- 指定连接数据库的 JDBC 驱动包所在位置,指定到你本机的完整路径 -->
<classpathEntry location="F:\mysql-connector-java-5.1.32.jar"/>
<!-- 配置 table 表信息内容体,targetRuntime 指定采用 MyBatis3 的版本 -->
<context id="tables" targetRuntime="MyBatis3">
<!-- 抑制生成注释,由于生成的注释都是英文的,可以不让它生成 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!-- 配置数据库连接信息 -->
<jdbcConnection driverClass="com.MysqL.jdbc.Driver"
connectionURL="jdbc:MysqL://localhost:3306/mydb1"
userId="root"
password="1234">
</jdbcConnection>
<!-- 生成 model 类,targetPackage 指定 model 类的包名, targetProject 指定
生成的 model 放在 eclipse 的哪个工程下面-->
<javaModelGenerator targetPackage="com.utkvrjan.springboot.model"
targetProject="E:\IdeaProjects\springbootProject\024-springboot-dubbo-ssm-interface\src\main\java">
<property name="enableSubPackages" value="false"/>
<property name="trimstrings" value="false"/>
</javaModelGenerator>
<!-- 生成 MyBatis 的 Mapper.xml 文件,targetPackage 指定 mapper.xml 文件的
包名, targetProject 指定生成的 mapper.xml 放在 eclipse 的哪个工程下面 -->
<sqlMapGenerator targetPackage="com.utkvrjan.springboot.mapper"
targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 生成 MyBatis 的 Mapper 接口类文件,targetPackage 指定 Mapper 接口类的包
名, targetProject 指定生成的 Mapper 接口放在 eclipse 的哪个工程下面 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="com.utkvrjan.springboot.mapper" targetProject="src/main/java">
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 数据库表名及对应的 Java 模型类名 -->
<table tableName="t_student" domainObjectName="Student"
enableCountByExample="false"
enableupdateByExample="false"
enableDeleteByExample="false"
enableSelectByExample="false"
selectByExampleQueryId="false"/>
</context>
</generatorConfiguration>
c.服务消费者
StudentController
package com.utkvrjan.springboot.web;
import com.alibaba.dubbo.config.annotation.Reference;
import com.utkvrjan.springboot.model.Student;
import com.utkvrjan.springboot.service.StudentService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class StudentController {
//check=false当服务消费者开启时不去检查提供者是否开启,只会在调用服务时再去检查
@Reference(interfaceName = "com.utkvrjan.springboot.service.StudentService",version="1.0.0",check = false)
private StudentService studentService;
@RequestMapping("/student/detail/{id}")
public String studentDetail(Model model, @PathVariable("id") Integer id){
Student student=studentService.queryStudentById(id);
model.addAttribute("student",student);
return "studentDetail";
}
@GetMapping(value="/student/all/count")
public @ResponseBody Object allStudentCount(){
Integer allStudentCount=studentService.queryAllStudentCount();
return "学生总人数为:"+allStudentCount;
}
}
Application
package com.utkvrjan.springboot;
import com.alibaba.dubbo.spring.boot.annotation.EnabledubboConfiguration;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnabledubboConfiguration
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
application.properties
# 设置内嵌Tomcat
server.port=8080
server.servlet.context-path=/
#设置dubbo设置
spring.application.name=026-springboot-dubbo-ssm-consumer
#设置注册中心
spring.dubbo.registry=zookeeper://localhost:2181
# 配置视图解析器
spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp
studentDetail.jsp
<%--
Created by IntelliJ IDEA.
User: 30207
Date: 2021/3/15
Time: 23:24
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>学生详情</title>
</head>
<body>
<h3>学生编号:${student.id}</h3>
<h3>学生姓名:${student.name}</h3>
<h3>学生年龄:${student.age}</h3>
</body>
</html>
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.utkvrjan.springboot</groupId>
<artifactId>026-springboot-dubbo-ssm-consumer</artifactId>
<version>1.0.0</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--dubbo集成springboot起步依赖-->
<dependency>
<groupId>com.alibaba.spring.boot</groupId>
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>2.0.0</version>
</dependency>
<!--注册中心-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<!--接口工程-->
<dependency>
<groupId>com.utkvrjan.springboot</groupId>
<artifactId>024-springboot-dubbo-ssm-interface</artifactId>
<version>1.0.0</version>
</dependency>
<!--Springboot集成JSP,仅仅只是展示JSP页面需要添加解析jsp页面的依赖-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
<build>
<!--虽然加了依赖,但是它要求JSP必须指定编译到特定的位置-->
<resources>
<resource>
<directory>src/main/webapp</directory>
<targetPath>meta-inf/resources</targetPath>
<includes>
<include>*.*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>