SpringBoot集成MyBatis(微服务)
来源:互联网 发布:linux 重置root密码 编辑:程序博客网 时间:2024/05/16 11:49
今天小编介绍的是简单学习springboot后,与mybatis集成所做的CRUD操作的一个微服务。以下是小编对自己做的这个小工程的详解,不足之处还请各位大神多多指教。
【项目介绍】
网上书城一个简单增删改查模块的微服务(这里仅提供后台模块的代码)
【工具】小编使用的是 IntelliJ IDEA,建议去官网下载Ultimate版本的,可以做RESTful Web Service测试,比Community版本的功能更多。
【详解】
一、相关配置:
1.在pom.xml文件中描述pom依赖
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.4.RELEASE</version>
</parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency><!-- mysql驱动 --><dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency><!--数据源--><dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency><!-- spring-boot mybatis依赖--><dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency><!--分页插件--><dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.1.3</version>
</dependency></dependencies>
2.在application.yml中配置数据源等(在application.properties配置也可以,.yml文件的层级关系比较清晰,配置信息多时建议用yml配置)
spring:
application:
name: springbootmybatisbookshop #全小写
datasource:
name: test
url: jdbc:mysql://localhost:3306/test
username: root
password: 123
type: com.alibaba.druid.pool.DruidDataSource # 使用druid数据源
driver-class-name: com.mysql.jdbc.Driver# location
mybatis:
mapper-locations: classpath:mapper/*.xml
config-location: classpath:mybatisConfiguration.xml
二、java代码
代码部分分为mapper、service、controller三个层次。以及bean类(demo)。
//demo
private int id;
private String name;
private String type;
private Double price;//setter getter//tostring
//mapper
public interface BookMapper {public void add(Book book);public void delete(int id);public void update(Book book);public List<Book> getByBookName(String name);public List<Book> getByBookType(String type);public List<Book> getAll();
//bookshopMapper.xml
//尽量少用*,而用字段名替代,以便于对后期代码的维护
//sql语句配置方式有两种:基于注解与基于xml配置;此处我们使用注解方式配置以便于维护
<resultMap id="book" type="BK">
<id column="id" property="id"></id>
<result column="name" property="name"></result>
<result column="type" property="type"></result>
<result column="price" property="price"></result>
</resultMap><!-- id对应mapper中的方法名 传入的参数值用占位符表示#{name}--><select id="getByBookName" parameterType="String" resultMap="book">
select * from book where name=#{name}
</select><!-- select id,name,type,price from book where type=#{type} 最好用字段名替代* --><select id="getByBookType" parameterType="String" resultMap="book">
select * from book where type=#{type}
</select> <select id="getAll" resultMap="book">
select * from book
</select> <insert id="add" parameterType="BK" useGeneratedKeys="true" keyProperty="id">
insert into book(id,name,type,price) values (#{id},#{name},#{type},#{price})
</insert><!-- 考虑字段值为空的情况 --><update id="update" parameterType="BK">
update book
<set>
<if test="name != null and name != ''">
name = #{name},
</if>
<if test="type != null and type != ''">
type = #{type},
</if>
<if test="price != null and price != ''">
price = #{price},
</if>
</set>
where id = #{id}
</update><delete id="delete" parameterType="int">
delete from book where id=#{id}
</delete>
</mapper>
//mybatisConfiguration.xml
<properties>
<property name="dialect" value="mysql" />
</properties> <typeAliases>
<typeAlias alias="BK" type="bookShop.demo.Book"/>
</typeAliases>
</configuration>
//service
public class BookSerevice {@Autowired
private BookMapper bookMapper;public List<Book> getByBookName(String name){
return bookMapper.getByBookName(name);
}public List<Book> getByBookType(String type){
return bookMapper.getByBookType(type);
}public List<Book> getAll(){
return bookMapper.getAll();
}@Transactional//添加事务管理
public void add(Book book){
bookMapper.add(book);
}@Transactional
public void delete(int id){
bookMapper.delete(id);
}@Transactional
public void update(Book book){
bookMapper.update(book);
}
//controller 资源的url设计为restful风格的
//http://www.cnblogs.com/loveis715/p/4669091.html
//该博客的REST简介很详细,感兴趣的喵可以戳进去看看
//注意 : BookController上面的注解是@RestController ,与@Controller区别开来
//return 时,@RestController 返回的是消息体 ; @Controller 返回的是一个转发页面
@RequestMapping("/book")
public class BookController {@Autowired
private BookSerevice bookSerevice;//用restful webservice 测试,method为post时要在Request Body的text中传参//在参数前加上注解 @RequestBody@RequestMapping(value="/",method = RequestMethod.POST)
public String add(@RequestBody Book book){
System.out.println(book);
bookSerevice.add(book);
return "sucess";
}//在url地址传参的参数前记得加上 @PathVariable@RequestMapping(value="/{id}",method = RequestMethod.DELETE)
public String delete(@PathVariable int id){
bookSerevice.delete(id);
return "sucess";
}@RequestMapping(value="/",method = RequestMethod.PUT)
public String update(@RequestBody Book book){
bookSerevice.update(book);
return "sucess";
}@RequestMapping(value="/name/{name}",method = RequestMethod.GET)
public List<Book> getByBookName(@PathVariable String name){
return bookSerevice.getByBookName(name);
}@RequestMapping(value="/type/{type}",method = RequestMethod.GET)
public List<Book> getByBookType(@PathVariable String type){
return bookSerevice.getByBookType(type);
}//分页显示@RequestMapping(value="/",method = RequestMethod.GET)
public List<Book> getAll(){
//PageHelper.startPage(pageNum,pageSize);
PageHelper.startPage(1,3);
return bookSerevice.getAll();
}
//App //运行测试即可
@MapperScan("bookShop.*")//扫描相应的class,主要是MyBatis的持久化类
public class App
{
public static void main( String[] args )
{
SpringApplication.run(App.class,args);
}
}
进行Restful WebService测试时,由于前后台进行数据交换/信息处理时用的最多的是json数据,因此一般要将左侧栏Headers的content-type设置为appllication/json.
以上代码还有单元测试没有加,也还有许多需要优化的地方,后续会持续优化、改正,同时非常期待各路大神的指点!
【总结】
该工程模块能够独立运行并进行简单业务处理,可作为一个简单微服务(MicroService)来调用,对外部开放其controller模块;又由于其调用是通过Http协议,需遵循REST规范,因此,微服务开发一般是restful编程(详见REST简介http://www.cnblogs.com/loveis715/p/4669091.html)
而webservice是要通过weblogic或者tomcat部署的工程,对外开放其service。我们可以理解为一个应用程序向外界暴露出的一个能通过Web调用的api,通过Http协议的post、get方法与远程机器进行交互。
springboot 是将spring的多个组件集成到一起,并提供大量的默认池集成以支持相关的服务。有关mybatis generator自动生成dao、model、mapping的详述会在后续文章介绍。(附官网地址:https://github.com/mybatis/generator)
阅读全文
2 0
- SpringBoot集成MyBatis(微服务)
- 微服务,微架构[六]之springboot集成mybatis
- SpringBoot~整合mybatis+微服务注册
- SpringBoot~整合mybatis+微服务注册
- 使用Springboot集成dubbox进行微服务治理
- 搭建Jenkins+SpringBoot+Docker的微服务持续集成框架
- 微服务,微架构[二]之springboot集成jdbc
- 微服务,微架构[四]之springboot集成Redis缓存
- 微服务,微架构[十三]springboot集成RabbitMQ
- 【Demo】springboot微服务+activemq消息中间件集成
- SOA微服务案例springboot+mybatis使用gradle构建案例
- springBoot集成mybatis
- SpringBoot集成mybatis
- springboot集成mybatis
- SpringBoot集成mybatis
- SpringBoot集成MyBatis
- SpringBoot -- 集成Mybatis/Druid
- SpringBoot集成MyBatis
- 解决VNC连接安了Ubuntu MATE系统的树莓派3b时出现灰屏的问题
- Unity 摄像机平滑跟随
- Ubuntu下Sublime Text 3解决无法输入中文的方法
- 原码、补码、、、
- linux下安装phantomjs
- SpringBoot集成MyBatis(微服务)
- 呕心沥血深入学正则和字符串
- LDAP 认证(Windows)
- jmeter(四):正则表达式提取器
- 如何用JS判断两个数字的大小
- QTableView 设置行间距
- SSIS+数据仓库系列--- 缓慢渐变维度 (Slowly Changing Dimension) 常见的三种类型及原型设计
- 解决SpringBoot更新数据到MySQL乱码问题
- Bootstrap modal不能垂直居中的解决办法