SpringBoot集成SSM、Dubbo、Redis、JSP的案例小結(jié)及思路講解
1.思路講解
這個(gè)案例其實(shí)就是SpringBoot集成SSM、Dubbo、Redis、JSP,看起來(lái)集成了一大堆,感覺(jué)挺麻煩的,但實(shí)際上并不是很麻煩,下面我來(lái)說(shuō)一下我的思路:
接口工程:存放實(shí)體bean和業(yè)務(wù)接口
服務(wù)提供者:它是一個(gè)SpringBoot框架web項(xiàng)目,集成MyBatis、Redis
1)pom文件中添加依賴:MyBatis、MySQL驅(qū)動(dòng)、Dubbo、zookeeper、redis、接口工程。
2)配置springboot核心配置文件(連接數(shù)據(jù)庫(kù)、連接redis、dubbo、內(nèi)嵌tomcat)
服務(wù)消費(fèi)者:它也是一個(gè)SpringBoot框架web項(xiàng)目,集成JSP、Dubbo
2)配置springboot核心配置文件(dubbo、內(nèi)嵌tomcat、視圖解析器)
1)pom文件中添加依賴:Dubbo、zookeeper、接口工程、解析jsp頁(yè)面的依賴。
文章比較長(zhǎng),因?yàn)榇a比較多,大家一定要有這個(gè)耐心去看完,相信我講的還是有點(diǎn)用的?。。?/strong>
2.案例分析
這里SpringBoot集成MyBatis,我用的是MyBatis逆向工程來(lái)直接生成的實(shí)體bean、dao、mapper,所以這里首先給出MyBatis逆向工程的配置文件。
這個(gè)文件主要是負(fù)責(zé)生成你項(xiàng)目中的實(shí)體bean、dao、mapper,那么再加上集成dubbo的情況下,我們的實(shí)體bean是需要放在接口工程中的,而dao、mapper則需要放在服務(wù)提供者中,所以在MyBatis逆向工程的配置文件中,需要將實(shí)體bean的生成位置改為第一個(gè)接口工程的絕對(duì)路徑。數(shù)據(jù)庫(kù)這里的表結(jié)構(gòu)和數(shù)據(jù),我就不再給出了,大家自行創(chuàng)建一下就可以了。
<?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> <!-- 指定連接數(shù)據(jù)庫(kù)的 JDBC 驅(qū)動(dòng)包所在位置,指定到你本機(jī)的完整路徑 --> <classPathEntry location="E:\mysql-connector-java-5.1.9.jar"/> <!-- 配置 table 表信息內(nèi)容體,targetRuntime 指定采用 MyBatis3 的版本 --> <context id="tables" targetRuntime="MyBatis3"> <!-- 抑制生成注釋?zhuān)捎谏傻淖⑨尪际怯⑽牡?,可以不讓它生?--> <commentGenerator> <property name="suppressAllComments" value="true"/> </commentGenerator> <!-- 配置數(shù)據(jù)庫(kù)連接信息 --> <jdbcConnection driverClass="com.mysql.jdbc.Driver" connectionURL="jdbc:mysql://localhost:3306/springboot" userId="root" password="12345678"> </jdbcConnection> <!-- 生成 entity 類(lèi),targetPackage 指定 entity 類(lèi)的包名, targetProject指定生成的 entity 放在 IDEA 的哪個(gè)工程下面--> <javaModelGenerator targetPackage="com.szh.springboot.entity" targetProject="D:\BaiduNetdiskDownload\014-springboot-ssm-dubbo-interface\src\main\java"> <property name="enableSubPackages" value="false"/> <property name="trimStrings" value="false"/> </javaModelGenerator> <!-- 生成 MyBatis 的 Mapper.xml 文件,targetPackage 指定 mapper.xml 文件的包名, targetProject 指定生成的 mapper.xml 放在 IDEA 的哪個(gè)工程下面 --> <sqlMapGenerator targetPackage="com.szh.springboot.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </sqlMapGenerator> <!-- 生成 MyBatis 的 Mapper 接口類(lèi)文件,targetPackage 指定 Mapper 接口類(lèi)的包名, targetProject 指定生成的 Mapper 接口放在 IDEA 的哪個(gè)工程下面 --> <javaClientGenerator type="XMLMAPPER" targetPackage="com.szh.springboot.mapper" targetProject="src/main/java"> <property name="enableSubPackages" value="false"/> </javaClientGenerator> <!-- 數(shù)據(jù)庫(kù)表名及對(duì)應(yīng)的 Java 模型類(lèi)名 --> <table tableName="t_student" domainObjectName="Student" enableCountByExample="false" enableUpdateByExample="false" enableDeleteByExample="false" enableSelectByExample="false" selectByExampleQueryId="false"/> </context> </generatorConfiguration>
2.1 接口工程
MyBatis逆向工程生成的實(shí)體bean。
package com.szh.springboot.entity; import java.io.Serializable; public class Student implements Serializable { private Integer id; private String name; private Integer age; //getter and setter }
接口服務(wù),其中有兩個(gè)方法。
package com.szh.springboot.service; import com.szh.springboot.entity.Student; /** * */ public interface StudentService { Student queryStudentById(Integer id); Integer queryAllStudentCount(); }
2.2 服務(wù)提供者
SpringBoot核心配置文件
# 配置內(nèi)嵌tomcat端口號(hào)和上下文根 server.port=8081 server.servlet.context-path=/ # 設(shè)置連接數(shù)據(jù)庫(kù)的信息 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=GMT%2B8 spring.datasource.username=root spring.datasource.password=12345678 # 設(shè)置dubbo spring.application.name=015-springboot-ssm-dubbo-provider spring.dubbo.server=true spring.dubbo.registry=zookeeper://localhost:2181 # 設(shè)置redis spring.redis.host=localhost spring.redis.port=6379
MyBatis逆向工程生成的dao接口和對(duì)應(yīng)的mapper映射文件(這里就做一個(gè)簡(jiǎn)單的案例,所以只用到了 selectByPrimaryKey、queryAllStudentCount 這兩個(gè)方法)
package com.szh.springboot.mapper; import com.szh.springboot.entity.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 queryAllStudentCount(); }
<?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.szh.springboot.mapper.StudentMapper"> <resultMap id="BaseResultMap" type="com.szh.springboot.entity.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.szh.springboot.entity.Student"> insert into t_student (id, name, age ) values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{age,jdbcType=INTEGER} ) </insert> <insert id="insertSelective" parameterType="com.szh.springboot.entity.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.szh.springboot.entity.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.szh.springboot.entity.Student"> update t_student set name = #{name,jdbcType=VARCHAR}, age = #{age,jdbcType=INTEGER} where id = #{id,jdbcType=INTEGER} </update> <select id="queryAllStudentCount" resultType="java.lang.Integer"> select count(*) from t_student </select> </mapper>
對(duì)接口工程中接口方法的實(shí)現(xiàn),其中包括注入數(shù)據(jù)庫(kù)持久層、注入redis模板類(lèi)對(duì)象。
package com.szh.springboot.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.szh.springboot.entity.Student; import com.szh.springboot.mapper.StudentMapper; import com.szh.springboot.service.StudentService; 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(interfaceClass = StudentService.class,version = "1.0.0",timeout = 15000) 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緩存中查詢,如果有:直接使用;如果沒(méi)有,去數(shù)據(jù)庫(kù)中查詢并存放到redis緩存中 Integer allStudentCount= (Integer) redisTemplate.opsForValue().get("allStudentCount"); //判斷是否有值 if (allStudentCount==null) { //此時(shí)為空,則去數(shù)據(jù)庫(kù)中查詢 allStudentCount=studentMapper.queryAllStudentCount(); //并存放到redis緩存中 redisTemplate.opsForValue().set("allStudentCount",allStudentCount,30, TimeUnit.SECONDS); } return allStudentCount; } }
pom文件
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.9</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <!-- 接口工程 --> <dependency> <groupId>com.szh.springboot</groupId> <artifactId>014-springboot-ssm-dubbo-interface</artifactId> <version>1.0.0</version> </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> <!--mybatis 代碼自動(dòng)生成插件--> <plugin> <groupId>org.mybatis.generator</groupId> <artifactId>mybatis-generator-maven-plugin</artifactId> <version>1.3.7</version> <configuration> <!--配置文件的位置--> <configurationFile>GeneratorMapper.xml</configurationFile> <verbose>true</verbose> <overwrite>true</overwrite> </configuration> </plugin> </plugins> </build>
SpringBoot項(xiàng)目啟動(dòng)入口類(lèi)
package com.szh.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.szh.springboot.mapper") @EnableDubboConfiguration public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
2.3 服務(wù)消費(fèi)者
SpringBoot核心配置文件
# 配置內(nèi)嵌tomcat端口號(hào)和上下文根 server.port=8080 server.servlet.context-path=/ # 設(shè)置dubbo spring.application.name=016-springboot-ssm-dubbo-consumer spring.dubbo.registry=zookeeper://localhost:2181 # 配置視圖解析器 spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsp
定義控制層,其中有兩個(gè)請(qǐng)求方法
package com.szh.springboot.controller; import com.alibaba.dubbo.config.annotation.Reference; import com.szh.springboot.entity.Student; import com.szh.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 { @Reference(interfaceClass = StudentService.class,version = "1.0.0",check = false) private StudentService studentService; @RequestMapping(value = "/student/detail/{id}") public String studentDetail(@PathVariable("id") Integer id, Model model) { 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 "學(xué)生總?cè)藬?shù)為:" + allStudentCount; } }
pom文件
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> <exclusions> <exclusion> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.4</version> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- 接口工程 --> <dependency> <groupId>com.szh.springboot</groupId> <artifactId>014-springboot-ssm-dubbo-interface</artifactId> <version>1.0.0</version> </dependency> </dependencies> <build> <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>
響應(yīng)的jsp頁(yè)面、SpringBoot項(xiàng)目啟動(dòng)入口類(lèi)
<%@ page contentType="text/html;charset=utf-8" language="java" %> <html> <head> <title>$</title> </head> <body> <h3>學(xué)生信息</h3> <div>學(xué)生編號(hào):${student.id}</div> <div>學(xué)生姓名:${student.name}</div> <div>學(xué)生年齡:${student.age}</div> </body> </html>
package com.szh.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); } }
2.4 啟動(dòng)測(cè)試?。?!
因?yàn)槲覀冞@個(gè)案例是SpringBoot集成SSM、Dubbo、Redis、JSP,同時(shí)使用注冊(cè)中心。所以啟動(dòng)的步驟是:
- 啟動(dòng)zookeeper注冊(cè)中心 zkServer.cmd(我這里為了考慮電腦性能,所以直接就在Windows上啟動(dòng)了,推薦是在Linux上啟動(dòng))
- 啟動(dòng)redis服務(wù)(redis-server.exe redis,windows.conf 、 redis-cli.exe -h 127.0.0.1 -p 6379)
- 啟動(dòng)服務(wù)提供者(對(duì)應(yīng)該工程的SpringBoot項(xiàng)目啟動(dòng)入口類(lèi))啟動(dòng)服務(wù)消費(fèi)者(對(duì)應(yīng)該工程的SpringBoot項(xiàng)目啟動(dòng)入口類(lèi))
測(cè)試結(jié)果中,可以看到,第一個(gè)請(qǐng)求結(jié)果拿到了學(xué)生信息,第二個(gè)請(qǐng)求結(jié)果也查詢出了學(xué)生數(shù)量,而且我們開(kāi)啟redis服務(wù)之后,可以看到發(fā)起第二個(gè)請(qǐng)求之后,redis緩存中已經(jīng)有了這個(gè) allStudentCount 數(shù)據(jù),經(jīng)過(guò)30秒之后,這個(gè)數(shù)據(jù)會(huì)被清除。
以上就是SpringBoot集成SSM、Dubbo、Redis、JSP的案例小結(jié)及思路講解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot集成SSM、Dubbo、Redis、JSP的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
實(shí)例講解Java設(shè)計(jì)模式編程中如何運(yùn)用代理模式
這篇文章主要介紹了Java設(shè)計(jì)模式編程中如何運(yùn)用代理模式,文中舉了普通代理和強(qiáng)制代理的例子作為代理模式的擴(kuò)展內(nèi)容,需要的朋友可以參考下2016-02-02SpringBoot2基于重復(fù)創(chuàng)建bean的問(wèn)題及解決
這篇文章主要介紹了SpringBoot2基于重復(fù)創(chuàng)建bean的問(wèn)題及解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06Spring Boot2深入分析解決java.lang.ArrayStoreException異常
這篇文章介紹了Spring Boot2深入分析解決java.lang.ArrayStoreException異常的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12SpringBoot 使用 @Value 注解讀取配置文件給靜態(tài)變量賦值
這篇文章主要介紹了SpringBoot 使用 @Value 注解讀取配置文件給靜態(tài)變量賦值,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11結(jié)合mybatis-plus實(shí)現(xiàn)簡(jiǎn)單不需要寫(xiě)sql的多表查詢
這篇文章主要給大家介紹了關(guān)于結(jié)合mybatis-plus實(shí)現(xiàn)簡(jiǎn)單不需要寫(xiě)sql的多表查詢的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用mybatis-plus具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09Springboot自動(dòng)配置與@Configuration配置類(lèi)詳解
這篇文章主要介紹了SpringBoot中的@Configuration與自動(dòng)配置,在進(jìn)行項(xiàng)目編寫(xiě)前,我們還需要知道一個(gè)東西,就是SpringBoot對(duì)我們的SpringMVC還做了哪些配置,包括如何擴(kuò)展,如何定制,只有把這些都搞清楚了,我們?cè)谥笫褂貌艜?huì)更加得心應(yīng)手2022-07-07springboot中配置好登錄攔截后,swagger訪問(wèn)不了問(wèn)題
這篇文章主要介紹了springboot中配置好登錄攔截后,swagger訪問(wèn)不了問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12