SpringBoot 整合JdbcTemplate
1.创建一个springboot_jdbc项目
2.导入依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>
3.创建一个entity实体类
public class Grade { private Integer grade_id; private String grade_name; }
4.创建一个dao层实体
IGradeDao
public interface IGradeDao { public int insertGrade(Grade grade); public int updateGrade(Grade grade); public int deleteGrade(Integer id); public List<Grade> findAll(); }
IGradeDaoImpl
@Repository public class IGradeDaoImpl implements IGradeDao { //导入JDBCTemplate模板 @Resource private JdbcTemplate jdbcTemplate; @Override public int insertGrade(Grade grade) { return jdbcTemplate.update("insert into Grade(grade_name) values(?)",grade.getGrade_name()); } @Override public int updateGrade(Grade grade) { return jdbcTemplate.update("update grade set grade_name=? where grade_id=?",grade.getGrade_name(),grade.getGrade_id()); } @Override public int deleteGrade(Integer id) { return jdbcTemplate.update("delete from grade where grade_id=?",id); } @Override public List<Grade> findAll() { //封装行数据映射 RowMapper<Grade> rowMapper=new RowMapper<Grade>() { @Override public Grade mapRow(ResultSet rs, int rowNum) throws SQLException { Grade grade=new Grade(rs.getInt("grade_id"),rs.getString("grade_name")); return grade; } }; return jdbcTemplate.query("select * from grade", rowMapper); } }
5.创建service
IGradeService
public interface IGradeService { public int insertGrade(Grade grade); public int updateGrade(Grade grade); public int deleteGrade(Integer id); public List<Grade> findAll(); }
IGradeServiceImpl
@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(); } }
6.创建controller控制器
@RestController public class JDBCTemplateController { @Resource 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(); } }
7.编写application.yml
spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///springdatajpa username: root password: 123456 ##更改Tomcat端口 server: port: 8081 ##指定当前工程项目访问地址 context-path: /jdbc
8.直接运行
@SpringBootApplication public class StartSpringBoot { public static void main(String[] args) { SpringApplication.run(StartSpringBoot.class,args); } }
分别访问http://localhost:8081/jdbc/insertGrade
返回数字1代表添加成功
http://localhost:8081/jdbc/updateGrade
http://localhost:8081/jdbc/deleteGrade
http://localhost:8081/jdbc/findAll
SpringBoot整合Mybatis
1.创建一个springboot_mybatis项目
2.导入依赖
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency>
3.创建application.yml文件
##数据源配置 spring: datasource: driver-class-name: com.mysql.jdbc.Driver url: jdbc:mysql:///springdatajpa username: root password: 123456 ##myabtis配置 mybatis: type-aliases-package: com.boot.entity mapper-locations: classpath:/mapper/*.xml ##开启日志 logging: level: com.boot.dao: debug
4.创建entity实体类
public class Grade { private Integer grade_id; private String grade_name; }
5.创建dao
@Repository @Mapper public interface IGradeDao { int insertGrade(Grade grade); int updateGrade(Grade grade); int deleteGrade(Integer id); List<Grade> findAll(); }
6.创建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(grade_name) values(#{grade_name}) </insert> <update id="updateGrade"> update grade set grade_name=#{grade_name} where grade_id=#{grade_id} </update> <delete id="deleteGrade"> delete from grade where grade_id=#{grade_id} </delete> <select id="findAll" resultType="com.boot.entity.Grade"> select * from grade </select> </mapper>
7.创建service
IGradeService
public interface IGradeService { int insertGrade(Grade grade); int updateGrade(Grade grade); int deleteGrade(Integer id); List<Grade> findAll(); }
IGradeServiceImpl
@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控制器
@RestController public class MybatisController { @Resource private IGradeService iGradeService; @RequestMapping("/insertGrade") public int insertGrade(){ return iGradeService.insertGrade(new Grade("S1")); } @RequestMapping("/updateGrade") public int updateGrade(){ return iGradeService.updateGrade(new Grade(2,"S2")); } @RequestMapping("/deleteGrade") public int deleteGrade(){ return iGradeService.deleteGrade(2); } @RequestMapping("/findAll") public List<Grade> findAll(){ return iGradeService.findAll(); } }
9.运行
@SpringBootApplication @MapperScan("com.boot.dao") public class StartSpringBoot { public static void main(String[] args) { SpringApplication.run(StartSpringBoot.class,args); } }
效果如上所示
SpringBoot整合Dubbo
1.创建springboot_dubbo_provider项目
2.导入依赖
<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>
3.创建application.yml
spring.dubbo.application.name=provider spring.dubbo.registry.address=zookeeper://127.0.0.1:2181 spring.dubbo.protocol.name=dubbo spring.dubbo.protocol.port=20880 ##com.mysql.jdbc.Driver spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql:///springdatajpa?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true spring.datasource.username=root spring.datasource.password=123456
4.创建service
public interface IDoSomeService { public String sayHi(); }
IDoSomeServiceImpl
//利用Dubbo暴露出一个接口 @Service(interfaceClass=IDoSomeService.class) @Component public class IDoSomeServiceImpl implements IDoSomeService { @Override public String sayHi() { System.out.println("生产者生产的IDoSomeService服务,中的sayHi方法"); return "SpringBoot Dubbo"; } }
5.运行
启动本地的zookeeper
启动项目
可以看到service已经暴露成功了
下面创建springboot_dubbo_consumer
创建application.properties
spring.dubbo.application.name=consumer
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
server.port=8081
创建service
public interface IDoSomeService { public String sayHi(); }
创建controller控制器
@RestController public class DubboController { @Reference private IDoSomeService iDoSomeService; @RequestMapping("/dubbo") public String dubbo(){ String returnValue = iDoSomeService.sayHi(); return returnValue; } }
直接启动