- 浏览: 575210 次
- 性别:
- 来自: 厦门
文章分类
- 全部博客 (669)
- oracle (36)
- java (98)
- spring (48)
- UML (2)
- hibernate (10)
- tomcat (7)
- 高性能 (11)
- mysql (25)
- sql (19)
- web (42)
- 数据库设计 (4)
- Nio (6)
- Netty (8)
- Excel (3)
- File (4)
- AOP (1)
- Jetty (1)
- Log4J (4)
- 链表 (1)
- Spring Junit4 (3)
- Autowired Resource (0)
- Jackson (1)
- Javascript (58)
- Spring Cache (2)
- Spring - CXF (2)
- Spring Inject (2)
- 汉字拼音 (3)
- 代理模式 (3)
- Spring事务 (4)
- ActiveMQ (6)
- XML (3)
- Cglib (2)
- Activiti (15)
- 附件问题 (1)
- javaMail (1)
- Thread (19)
- 算法 (6)
- 正则表达式 (3)
- 国际化 (2)
- Json (3)
- EJB (3)
- Struts2 (1)
- Maven (7)
- Mybatis (7)
- Redis (8)
- DWR (1)
- Lucene (2)
- Linux (73)
- 杂谈 (2)
- CSS (13)
- Linux服务篇 (3)
- Kettle (9)
- android (81)
- protocol (2)
- EasyUI (6)
- nginx (2)
- zookeeper (6)
- Hadoop (41)
- cache (7)
- shiro (3)
- HBase (12)
- Hive (8)
- Spark (15)
- Scala (16)
- YARN (3)
- Kafka (5)
- Sqoop (2)
- Pig (3)
- Vue (6)
- sprint boot (19)
- dubbo (2)
- mongodb (2)
最新评论
spring boot进行mybatis和JPA的整合
- 博客分类:
- web
- spring
- sprint boot
初识spring boot
Spring框架功能很强大,但是就算是一个很简单的项目,我们也要配置很多东西。因此就有了Spring Boot框架,它的作用很简单,就是帮我们自动配置。Spring Boot框架的核心就是自动配置,只要存在相应的jar包,Spring就帮我们自动配置。如果默认配置不能满足需求,我们还可以替换掉自动配置类,使用我们自己的配置。另外,Spring Boot还集成了嵌入式的Web服务器,系统监控等很多有用的功,让我们快速构建企业及应用程序。
1.启动类 需要把该类放到根包下面 因为都是从这个包开始往下开始扫描
2.在resources目录下面添加数据库等信息
3.mybatis配置类
4.添加mapper类
5.JPA实体类
6.JPA资源类
7.Controller类
Spring框架功能很强大,但是就算是一个很简单的项目,我们也要配置很多东西。因此就有了Spring Boot框架,它的作用很简单,就是帮我们自动配置。Spring Boot框架的核心就是自动配置,只要存在相应的jar包,Spring就帮我们自动配置。如果默认配置不能满足需求,我们还可以替换掉自动配置类,使用我们自己的配置。另外,Spring Boot还集成了嵌入式的Web服务器,系统监控等很多有用的功,让我们快速构建企业及应用程序。
1.启动类 需要把该类放到根包下面 因为都是从这个包开始往下开始扫描
@SpringBootApplication // 扫描JPA实体类 @EnableJpaRepositories(basePackages = "com.lanwx.entity") @ServletComponentScan // 存放mapper的目录 @MapperScan("com.lanwx.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
2.在resources目录下面添加数据库等信息
#数据库设置 spring.jpa.database=oracle spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driverClassName=oracle.jdbc.driver.OracleDriver spring.datasource.url=jdbc:oracle:thin:@xx:xx spring.datasource.username=xx spring.datasource.password=xx
3.mybatis配置类
@Configuration @ConditionalOnClass({ EnableTransactionManagement.class, EntityManager.class }) @MapperScan(basePackages={"com.xxx.mapper"}) public class MyBatisConfig implements EnvironmentAware{ private RelaxedPropertyResolver propertyResolver; @Override public void setEnvironment(Environment env) { this.propertyResolver = new RelaxedPropertyResolver(env, "spring.datasource."); } @Bean(name="dataSource", destroyMethod = "close", initMethod="init") @Primary public DataSource dataSource() { DruidDataSource datasource = new DruidDataSource(); datasource.setUrl(propertyResolver.getProperty("url")); datasource.setDriverClassName(propertyResolver.getProperty("driverClassName")); datasource.setUsername(propertyResolver.getProperty("username")); datasource.setPassword(propertyResolver.getProperty("password")); datasource.setValidationQuery("select count(1) from dual"); return datasource; } @Bean(name="sqlSessionFactory") @ConditionalOnMissingBean public SqlSessionFactory sqlSessionFactory() { try { SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); sessionFactory.setDataSource(dataSource()); sessionFactory.setTypeAliasesPackage("com.xxx.entity"); //添加XML目录 ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); sessionFactory.setMapperLocations(resolver.getResources("classpath:com/xxx/**/mapper/*.xml")); return sessionFactory.getObject(); } catch (Exception e) { return null; } } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } // @Bean // public RoundRobinRWRoutingDataSourceProxy roundRobinDataSouceProxy(){ // RoundRobinRWRoutingDataSourceProxy proxy = new RoundRobinRWRoutingDataSourceProxy(); // proxy.setWriteDataSource(writeDataSource); // proxy.setReadDataSoures(readDataSources); // proxy.setReadKey("READ"); // proxy.setWriteKey("WRITE"); // // return proxy; // } @Bean @ConditionalOnMissingBean public DataSourceTransactionManager transactionManager() { return new DataSourceTransactionManager(dataSource()); } }
4.添加mapper类
// 需要加Mapper注解 @Mapper public interface xxxxxMapper{ @Select("select count(1) from xx ") int xxxxxxx(); @Select("select * from xx where bui_id=#{xxxx}") xxx xxxxxxxx(@Param("xxx") String xxx); }
5.JPA实体类
// 注意引入包要正确 @Entity @Table(name="xxx") public class xxx implements Serializable{ private static final long serialVersionUID = 2425973233219442129L; @Id private String id ; private String name; public String getId() { return id; } public void setId(String id) { this.id = id; } @Column(name = "NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } }
6.JPA资源类
// 资源类只需要接口就能直接使用 @Repository @Table(name = "xxx") @Qualifier("xxxxx") public interface xxx extends JpaRepository<xxxx, String>{ public xxxx findOne(String id); @SuppressWarnings("all") public xxx save(Lanwx01 u); @Query("select t from xxx t where t.id=:id") public xx findUserByName(@Param("id") String id); }
7.Controller类
@RestController @EnableAutoConfiguration public class TestController { private static final Logger logger = LoggerFactory.getLogger(TestController.class); @Resource private MyService myService ; @Resource private xxxMapper xxxx; @Resource private xxxRepository xxxRepository; @RequestMapping("/xxx") public xxxx findzCC(@RequestParam String buiId){ System.out.println(xxxx.xxxx(buiId)); return xxx.findLanwx(buiId); } }
发表评论
文章已被作者锁定,不允许评论。
-
spring boot集成jsp
2018-11-21 16:53 7091.配置文件 server: port: 8080 ... -
Spring BeanFactoryPostProcessor和BeanPostProcessor的区别
2018-11-14 15:40 676链接:https://blog.csdn.net/caihai ... -
spring BeanPostProcessor理解
2018-11-14 11:31 292链接:https://blog.csdn.net/ginkgo ... -
Spring 源码解析之Initializer
2018-11-14 11:27 413链接:https://blog.csdn.net/ktlife ... -
spring boot AnnotationConfigApplicationContext的实例化过程
2018-11-10 14:18 1097链接:https://blog.csdn.net/chr1sg ... -
spring transaction同一个类不回滚解决方法
2018-10-11 10:59 7331.修改配置文件 <aop:aspectj-autopr ... -
Spring @Transaction学习
2018-10-08 10:36 2621.考虑有下面这么一个类 public class Foo ... -
spring mvc i18n国际化学习(spring:message)
2018-06-09 09:35 602spring.xml文件中配置: <!-- 存储区域 ... -
html pre标签使用(保留空格和回车等操作)
2018-05-15 08:37 2384pre 元素可定义预格式化的文本。被包围在 pre 元素中的文 ... -
Spring Boot Oauth2.0授权服务器
2018-05-11 14:19 1606什么是OAuth? OAuth(Open Authoriza ... -
Spring Boot @Import注解(将指定类实例注入到IOC容器中)
2018-05-09 10:20 1568SpringBoot 的 @Import 用于将指定的类实例注 ... -
Spring Boot @Conditional注解
2018-05-09 10:15 1787Spring Boot的强大之处在于使用了Spring 4框架 ... -
Spring Boot自定义starter pom实例(/META-INFO/spring.factory文件)
2018-05-09 09:48 1082自定义starter pom 自己实现一个简单的例子,当某个类 ... -
java RESTful 详解
2018-04-27 11:35 591(1)每一个URI代表一种资源,独一无二; (2)客户端 ... -
Spring Boot自动配置原理(@Conditional @Import)
2018-04-26 14:45 1260Springboot的自动配置是SpringBoot的关键,主 ... -
Spring Boot优缺点总结
2018-04-16 10:25 1502优点: 1.去除了大量的xml配置文件 2.简化 ... -
SpringBoot JPA @Transaction 知识学习
2018-03-16 09:09 730一、事务相关概念 1、事务的特点 原子性:事务是一个原子操 ... -
Sprint @Query注解的用法(nativeQuery=true/false)(Spring Data JPA)
2018-03-15 16:33 35781. 一个使用@Query注解的简单例子 @Query(val ... -
Spring Boot JpaRepository知识学习(Spring Data JPA)
2018-03-14 11:17 16131.Spring Data所解决的问题 Spring Dat ... -
SpringCloud Hystrix知识学习(防止雪崩效应)
2018-03-13 14:57 877一、Hystrix说明 1.服务雪崩效应:是一种因服务提供者的 ...
相关推荐
在Spring Boot中,可以通过整合MyBatis和JPA来与数据库进行交互。以下是分别整合MyBatis和JPA的步骤: 整合MyBatis: 1. 添加依赖: 在项目的 pom.xml 文件中添加MyBatis和MySQL依赖 2.配置数据源: 在 application....
这个demo同时整合了springboot+JPA+mybatis这个两个orm框架。
项目描述 学生成绩管理系统,有三...spring boot+spring mvc+mybatis+layui+jquery+thymeleaf http://localhost:8080/Sys/loginView 管理员账号 admin admin 老师登录 2020031920 111111 学生账号登录 20200319 111111
springboot整合mybatis整合jpa使用entityManger整合readis以及微信支付和支付宝支付
spring-boot-jpa-thymeleaf-curd:spring boot + jpa + thymeleaf 增删改查示例 spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-web...
spring-boot-jpa-thymeleaf-curd:spring boot + jpa + thymeleaf 增删改查示例 spring-boot-rabbitmq:spring boot和rabbitmq各种消息应用案例 spring-boot-scheduler:spring boot和定时任务案例 spring-boot-...
Spring Boot 过滤器和拦截器 Spring Boot 全局异常处理 Spring Boot 参数校验 Spring Boot OpenAPI 文档集成 Spring Boot 集成 Mybatis Spring Boot 集成 Mybatis-plus Spring Boot JPA - 全自动 ORM Spring Boot ...
基于Spring Boot + VUE CLI@3 框架开发的分布式文件系统,旨在为用户和企业提供一个简单、方便的文件存储方案,能够...后台:Spring Boot、MyBatis、JPA、JWT;数据库 : MySQL;数据结构:递归算法,树的遍历和插入...
spring-boot-jpa-thymeleaf-crud Spring Boot+JPA+Thymeleaf实现增删改查示例 spring-boot-mybatis-thymeleaf-crud Spring Boot+MyBatis+Thymeleaf实现增删改查示例 spring-boot-docker Sping Boot使用远程Docker...
spring-boot + jpa + mybatis + gradle 概述: 本项目为gradle项目 基础框架为spring-boot ORM框架选用 JPA + MyBatis 内含 mybatis 物理分页插件
- [spring-boot-mybatis-xml-mulidatasource](https://github.com/ityouknow/spring-boot-examples/tree/master/spring-boot-mybatis/spring-boot-mybatis-xml-mulidatasource):Spring Boot + Mybatis (Xml 版) 多...
1.Spring Boot 整合 JDBC 案例 2.Spring Boot 整合 Druid 数据源案例 3.Spring Boot 整合 MyBatis 案例 4.Spring Boot 整合 JPA 案例
毕设项目:后端springboot + mybatis + jpa,前端vue实现的简易网盘系统.zip 1、该资源内项目代码经过严格调试,下载即用确保可以运行! 2、该资源适合计算机相关专业(如计科、人工智能、大数据、数学、电子信息等)...
spring-boot和Mybatis集成,并配置多数据源、事务和连接池,使用jpa,xml方式访问数据库以及直接使用SqlSessionTemplate访问数据库的demo。
Spring Boot Druid Mybatis Schedule动态数据源JPA 本系列是spring-boot相关的一些列子,比如spring-boot集成druid,以及druid的动态数据源切换, spring-boot 集成mybatis,spring-boot集成定时器等等 1,弹簧靴...
maven+springboot+jpa+shiro权限管理内有数据库sql,只要哪里这个基础代码之后的功能添加很方便,适合快速实现项目
本系统作为vue3初学者的入门系统,通过开发经典的《宿舍管理系统》模拟项目,使vue2的初学者快速掌握从无到有的搭建一套vue3+element-plus前端+spring boot后端+mysql数据库的前后端分离的现代主流项目架构。...
spring boot 实践学习案例,与其它组件结合如 mybatis、jpa、dubbo、redis、mongodb、memcached、kafka、rabbitmq、activemq、elasticsearch、security、shiro等 #### Spring Boot 版本 - 2.0.3.RELEASE #### 模块...