SpringBoot3(springboot_jdbctemplate以及MyBatis和Dubbo整合)
一、JDBCTemplate
1、导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
2、配置application文件
3、创建启动类
package com.boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class StartSpringBoot { public static void main(String[] args){ SpringApplication.run(StartSpringBoot.class,args); } }
4、创建实体层
package com.boot.entity; public class Grade { private Integer gradeId; private String gradeName; public Grade() { } public Grade(String gradeName) { this.gradeName = gradeName; } public Grade(Integer gradeId, String gradeName) { this.gradeId = gradeId; this.gradeName = gradeName; } public Integer getGradeId() { return gradeId; } public void setGradeId(Integer gradeId) { this.gradeId = gradeId; } public String getGradeName() { return gradeName; } public void setGradeName(String gradeName) { this.gradeName = gradeName; } }
5、创建Dao层
package com.boot.dao; import com.boot.entity.Grade; import java.util.List; public interface IGradeDao { public int insertGrade(Grade grade); public int updateGrade(Grade grade); public int deleteGrade(Integer id); public List<Grade> getAll(); }
6、创建Dao层实现类
package com.boot.dao.impl; import com.boot.dao.IGradeDao; import com.boot.entity.Grade; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowMapper; import org.springframework.stereotype.Repository; import javax.annotation.Resource; import java.sql.ResultSet; import java.sql.SQLException; import java.util.List; @Repository public class IGradeDaoImpl implements IGradeDao { //导入JDBCTemplate模板 @Resource private JdbcTemplate jdbcTemplate; @Override public int insertGrade(Grade grade) { return jdbcTemplate.update("insert into Grade(GradeName) values (?)",grade.getGradeName()); } @Override public int updateGrade(Grade grade) { return jdbcTemplate.update("update Grade set GradeName=? where GradeId=?",grade.getGradeName(),grade.getGradeId()); } @Override public int deleteGrade(Integer id) { return jdbcTemplate.update("delete from Grade where GradeId=?",id); } @Override public List<Grade> getAll() { //fengxhaung封装行数据映射 RowMapper<Grade> rowMapper=new RowMapper<Grade>() { @Override public Grade mapRow(ResultSet rs, int i) throws SQLException { Grade grade=new Grade(rs.getInt("GradeId"),rs.getString("GradeName")); return grade; } }; return jdbcTemplate.query("select * from Grade",rowMapper); } }
7、创建Service层
package com.boot.service; import com.boot.entity.Grade; import java.util.List; public interface IGradeService { public int insertGrade(Grade grade); public int updateGrade(Grade grade); public int deleteGrade(Integer id); public List<Grade> getAll(); }
8、创建Service实现类
package com.boot.service.impl; import com.boot.dao.IGradeDao; import com.boot.entity.Grade; import com.boot.service.IGradeService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service("iGradeService") public class IGradeServiceImpl implements IGradeService { @Resource //植入Dao层对象 private IGradeDao iGradeDao; @Override public int insertGrade(Grade grade) { return iGradeDao.insertGrade(grade); } @Override public int updateGrade(Grade grade) { return iGradeDao.updateGrade(grade); } @Override public int deleteGrade(Integer id) { return iGradeDao.deleteGrade(id); } @Override public List<Grade> getAll() { return iGradeDao.getAll(); } }
9、创建Controller层
package com.boot.controller; import com.boot.entity.Grade; import com.boot.service.IGradeService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; @RestController public class IGradeController { //导入Service层 @Resource private IGradeService iGradeService; @RequestMapping("/insertGrade") public int insertGrade(Grade grade){ return iGradeService.insertGrade(new Grade("S1")); } @RequestMapping("/updateGrade") public int updateGrade(Grade grade){ return iGradeService.updateGrade(new Grade(2,"S1")); } @RequestMapping("/deleteGrade") public int deleteGrade(){ return iGradeService.deleteGrade(6); } @RequestMapping("/getAll") public List<Grade> getAll(){ return iGradeService.getAll(); } }
10、实现效果如下
二、MyBatis整合
1、导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
2、配置application文件
3、创建entity层
package com.boot.entity;
import java.io.Serializable;
public class Grade implements Serializable {
private Integer gradeId;
private String gradeName;
public Grade() {
}
public Grade(String gradeName) {
this.gradeName = gradeName;
}
public Grade(Integer gradeId, String gradeName) {
this.gradeId = gradeId;
this.gradeName = gradeName;
}
public Integer getGradeId() {
return gradeId;
}
public void setGradeId(Integer gradeId) {
this.gradeId = gradeId;
}
public String getGradeName() {
return gradeName;
}
public void setGradeName(String gradeName) {
this.gradeName = gradeName;
}
}
4、创建Dao层
package com.boot.dao; import com.boot.entity.Grade; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface IGradeDao { public int insertGrade(Grade grade); public int updateGrade(Grade grade); public int deleteGrade(Integer id); public List<Grade> findAll(); }
5、创建Dao层小配置文件(在resources目录下创建mapper目录)
<?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.boot.dao.IGradeDao" > <insert id="insertGrade"> insert into Grade(GradeName) values(#{gradeName}) </insert> <insert id="updateGrade"> update set GradeName=#{gradeName} from Grade where GradeId=#{gradeId} </insert> <insert id="deleteGrade"> delete from Grade where GradeId=#{gradeId} </insert> <select id="findAll" resultType="com.boot.entity.Grade"> select * from Grade </select> </mapper>
6、创建Service层
package com.boot.service; import com.boot.entity.Grade; import java.util.List; public interface IGradeService { public int insertGrade(Grade grade); public int updateGrade(Grade grade); public int deleteGrade(Integer id); public List<Grade> findAll(); }
7、创建Service实现层
package com.boot.service.impl; import com.boot.dao.IGradeDao; import com.boot.entity.Grade; import com.boot.service.IGradeService; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; @Service("iGradeService") public class IGradeServiceImpl implements IGradeService { @Resource private IGradeDao iGradeDao; @Override public int insertGrade(Grade grade) { return iGradeDao.insertGrade(grade); } @Override public int updateGrade(Grade grade) { return iGradeDao.updateGrade(grade); } @Override public int deleteGrade(Integer id) { return iGradeDao.deleteGrade(id); } @Override public List<Grade> findAll() { return iGradeDao.findAll(); } }
8、创建Controller层
package com.boot.controller; import com.boot.entity.Grade; import com.boot.service.IGradeService; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; import java.util.List; @RestController public class MybatisController { @Resource(name = "iGradeService") private IGradeService iGradeService; @RequestMapping("/insertGrade") public int insertGrade(){ return iGradeService.insertGrade(new Grade("S1")); } @RequestMapping("/updateGrade") public int updateGrade(){ return iGradeService.updateGrade(new Grade(10012,"S2")); } @RequestMapping("/deleteGrade") public int deleteGrade(){ return iGradeService.deleteGrade(10012); } @RequestMapping("/findAll") public List<Grade> findAll(){ return iGradeService.findAll(); } }
9、实现效果如下
三、Dubbo整合
1、导入依赖
<!--dubbo依赖--> <dependency> <groupId>com.101tec</groupId> <artifactId>zkclient</artifactId> <version>0.10</version> </dependency> <dependency> <groupId>com.alibaba.spring.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <!--MySQL--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.38</version> </dependency>
2、创建消费者springboot-dubbo-consumer
(1)配置application文件
(2)创建启动类
package com.boot; import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableDubboConfiguration public class DubboConsumer { public static void main(String[] args) { SpringApplication.run(DubboConsumer.class,args); } }
(3)创建Service层
package com.boot.service; public interface IDoSomeService { public String sayHi(); }
(4)创建Controller层
package com.boot.controller; import com.boot.service.IDoSomeService; import jdk.nashorn.internal.ir.annotations.Reference; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class DubboController { @Reference private IDoSomeService iDoSomeService; @RequestMapping("/dubbo") public String dubbo(){ String returnValue=iDoSomeService.sayHi(); return returnValue; } }
3、创建生产者springboot-dubbo-provider
(1)配置文件
(2)创建DubboProvider层
package com.boot.com.boot.service; import org.springframework.boot.SpringApplication; public class DubboProvider { public static void main(String[] args){ SpringApplication.run(DubboProvider.class,args); System.out.println("Dubbo接口暴露成功!"); } }
(3)创建IDoSomeService层
package com.boot.com.boot.service; public interface IDoSomeService { public String sayHi(); }
(4)创建IDoSomeService实现层
package com.boot.com.boot.service.impl; import com.alibaba.dubbo.config.annotation.Service; import com.boot.com.boot.service.IDoSomeService; import org.springframework.stereotype.Component; //利用Dubbo暴露出一个接口 @Service(interfaceClass=IDoSomeService.class) @Component public class IDoSomeServiceImpl implements IDoSomeService { @Override public String sayHi() { System.out.println("生产者生产的IDoSomeService服务中的sayHi方法"); return "SpringBoot Dubbo"; } }
4、实现效果如下