首页 文章 精选 留言 我的

精选列表

搜索[SpringBoot4],共10000篇文章
优秀的个人博客,低调大师

第五篇:SpringBoot 2.x整合BeetlSQL

image.png 上图是 BeetlSQL官网中对BeetlSQL的介绍,简单来说我们可以得到几个点 开发效率高 维护性好 性能数倍于JPA MyBatis 关于BeetlSQL的更多介绍大家可以去到官网去看看,接下来我们来看看如何把这个DAO工具整合到项目中 pom.xml <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 引入beetlsql --> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetlsql</artifactId> <version>2.10.34</version> </dependency> <!-- 引入beetl --> <dependency> <groupId>com.ibeetl</groupId> <artifactId>beetl</artifactId> <version>2.9.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> sql文件,我这里用的是mysql CREATE TABLE `test`.`Untitled` ( `id` int(11) NOT NULL AUTO_INCREMENT, `username` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `nickname` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL, `age` int(11) NULL DEFAULT 18, `cdate` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP(0), `udate` timestamp(0) NULL DEFAULT CURRENT_TIMESTAMP(0), PRIMARY KEY (`id`) USING BTREE ) ENGINE = InnoDB AUTO_INCREMENT = 1 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = Dynamic; User.java package com.priv.gabriel.entity; /** * Created with Intellij IDEA. * * @Author: Gabriel * @Date: 2018-10-14 * @Description: */ public class User { private long id; private String username; private String nickname; private int age; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getNickname() { return nickname; } public void setNickname(String nickname) { this.nickname = nickname; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", nickname='" + nickname + '\'' + ", age=" + age + '}'; } } 在这里有两个分支,一种是通过sqlManager来操作,另一种是整合mapper,在这里我们现看看第一种方式 SQLManager方式 UserControllerForSQLManager.java package com.priv.gabriel.controller; import com.priv.gabriel.entity.User; import com.priv.gabriel.repository.UserRepository; import org.beetl.sql.core.SQLManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * Created with Intellij IDEA. * * @Author: Gabriel * @Date: 2018-10-14 * @Description: */ @RestController @RequestMapping("/sqlManager/users") public class UserControllerForSQLManager { //自动注入即可 @Autowired private SQLManager sqlManager; /* * @Author Gabriel * @Description 根据主键查找记录 * @Date 2018/10/16 * @Param [id] 主键 * @Return void */ @RequestMapping(value = "/{id}",method = RequestMethod.GET) public User selectUserById(@PathVariable("id")int id){ //如果没有查到数据则抛出异常 //return sqlManager.unique(User.class,id); //如果没有查到数据则返回null return sqlManager.single(User.class,id); } /* * @Author Gabriel * @Description 查询所有 * @Date 2018/10/16 * @Param [] * @Return java.util.List<com.priv.gabriel.entity.User>*/ @RequestMapping(value = {"","/"},method = RequestMethod.GET) public List<User> getUsers(){ //获取所有数据 //return sqlManager.all(User.class); //查询该表的总数 //return sqlManager.allCount(User.class); //获取所有数据 分页方式 return sqlManager.all(User.class,1,2); } /* * @Author Gabriel * @Description 单表条件查询 * @Date 2018/10/16 * @Param [] * @Return void*/ public void singletonTableQuery(){ //通过sqlManager.query()可以在后面追加各种条件 sqlManager.query(User.class).andLike("username","admin").orderBy("age").select(); } /* * @Author Gabriel * @Description 新增数据 * @Date 2018/10/16 * @Param [user] * @Return void*/ @RequestMapping(value = {"","/"},method = RequestMethod.POST) public void addUser(User user){ //添加数据到对应表中 //sqlManager.insert(User.class,user); //添加数据到对应表中,并返回自增id sqlManager.insertTemplate(user,true); System.out.println(user.getId()); System.out.println("新增成功"); } /* * @Author Gabriel * @Description 根据主键修改 * @Date 2018/10/16 * @Param [user] * @Return java.lang.String*/ @RequestMapping(value = {"","/"},method = RequestMethod.PUT) public String updateById(User user){ //根据id修改,所有值都参与更新 //sqlManager.updateById(user); //根据id修改,属性为null的不会更新 if(sqlManager.updateTemplateById(user)>0){ return "修改成功"; }else{ return "修改失败"; } } /* * @Author Gabriel * @Description 删除记录 * @Date 2018/10/16 * @Param [id] * @Return java.lang.String*/ @RequestMapping(value = "/id",method = RequestMethod.DELETE) public String deleteById(@PathVariable("id") int id){ if(sqlManager.deleteById(User.class,id) >0 ){ return "删除成功"; }else{ return "删除失败"; } } } Mapper方式 如果要使用mapper方式,则需要新建一个mapper接口,并继承BaseMapper<T> UserRepository.java package com.priv.gabriel.repository; import com.priv.gabriel.entity.User; import org.beetl.sql.core.mapper.BaseMapper; /** * Created with Intellij IDEA. * * @Author: Gabriel * @Date: 2018-10-14 * @Description: */ public interface UserRepository extends BaseMapper<User>{ } UserControllerForMapper.java package com.priv.gabriel.controller; import com.priv.gabriel.entity.User; import com.priv.gabriel.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** * Created with Intellij IDEA. * * @Author: Gabriel * @Date: 2018-10-14 * @Description: */ @RestController @RequestMapping("/mapper/users") public class UserControllerForMapper { @Autowired private UserRepository repository; @RequestMapping(value = {"","/"},method = RequestMethod.POST) public void addUser(User user){ repository.insert(user); } @RequestMapping(value = {"","/"},method = RequestMethod.DELETE) public String deleteUserById(User user){ if(repository.deleteById(user) >0 ){ return "删除成功"; }else{ return "删除失败"; } } @RequestMapping(value = {"","/"},method = RequestMethod.PUT) public String updateUserById(User user){ //repository.updateById(user) if(repository.deleteById(user) > 0){ return "修改成功"; }else{ return "修改失败"; } } @RequestMapping(value = "/{id}",method = RequestMethod.GET) public User selectUserById(@PathVariable("id")int id){ //repository.unique(id); return repository.single(id); } @RequestMapping(value = {"","/"},method = RequestMethod.GET) public List<User> getUsers(){ //repository.all(1,2); //repository.allCount(); return repository.all(); } } 两种方式都介绍完毕了,但是BeetlSQL的重点部分还不在这,BeetlSQL的重点是可以创建一个SQL模板,到这大家可能会想,不就是个xml嘛,mybatis就有呀,不一样的地方就在这了,BeetlSQL的SQL模板是这样的 selectByTest === select * from user where 1=1 怎么样,是不是眼前一亮,很明显 selectByTest 是这条SQL语句的id , ===的作用是代表id和内容的分割,而最后的部分当然就是SQL语句啦 然后简单介绍一下调用SQL模板的方式 SQLManager方式 @RequestMapping(value = "/test",method = RequestMethod.GET) public List<User> getUsersByTest(){ return sqlManager.select("user.selectByTest",User.class); } 在SQLManager的方式中,通过sqlManager.select("模板id",类型)的方式直接调用 Mapper的方式 @SqlResource("user") public interface UserRepository extends BaseMapper<User>{ List<User> selectByTest(); } 在Mapper的方式,需要先建立一个xxx.md的SQL模板文件,通过@SqlResource(模板文件名)这个注解找到模板文件,再在mapper中写入与模板文件中同名的方法,即可在外部调用 注意,BeetlSQL的模板文件位置默认在resource/sql/xxx.md中,好啦,关于BeetlSQL的介绍就到这里。 BeetlSQL的详细介绍Beetl官方文档BeetlSQL官方文档项目点此下载

优秀的个人博客,低调大师

精通SpringBoot——第四篇:Spring事件 Application Event

Spring的事件为Bean与Bean之间的通信提供了支持,当我们系统中某个Spring管理的Bean处理完某件事后,希望让其他Bean收到通知并作出相应的处理,这时可以让其他Bean监听当前这个Bean所发送的事件。 要实现事件的监听,我们要做两件事:1:自定义事件,继承ApplicationEvent接口2:定义事件监听器,实现ApplicationListener3:事件发布类 /** * @TODO // 自定义事件,继承ApplicationEvent接口 * @Author Lensen * @Date 2018/7/22 * @Description */ public class SendMsgEvent extends ApplicationEvent { private static final long serialVersionID = 1L; // 收件人 public String receiver; // 收件内容 public String content; public SendMsgEvent(Object source) { super(source); } public SendMsgEvent(Object source, String receiver, String content) { super(source); this.receiver = receiver; this.content = content; } public void output(){ System.out.println("I had been sand a msg to " + this.receiver); } } /** * @TODO //定义事件监听器,实现ApplicationListener * @Author Lensen * @Date 2018/7/22 * @Description */@Component public class MsgListener implements ApplicationListener<SendMsgEvent> { @Override public void onApplicationEvent(SendMsgEvent sendMsgEvent) { sendMsgEvent.output(); System.out.println(sendMsgEvent.receiver + "received msg : " + sendMsgEvent.content ); } } 事件发布类 @Component public class Publisher { @Autowired ApplicationContext applicationContext; public void publish(Object source, String receiver, String content){ applicationContext.publishEvent(new SendMsgEvent(source, receiver, content)); } } 测试消息:WebConfig.class主要是为了扫描Publisher 和Listener类。里面有两个注解@ComponenScan和@Configuration。 public static void main(String[] args) { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(WebConfig.class); Publisher publisher = applicationContext.getBean(Publisher.class); publisher.publish("Hello,World!","Mr.Lensen", "I Love U"); } 结果: I had been sand a msg to Mr.Lensen Mr.Lensen received msg : I Love U

资源下载

更多资源
优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

用户登录
用户注册