本次只是简单的运用SpringBoot搭建框架,对其原理并不做深入的探究

1.POM文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5.  
  6. <groupId>com.hxyz</groupId>
  7. <artifactId>media</artifactId>
  8. <version>0.0.1-SNAPSHOT</version>
  9. <packaging>jar</packaging>
  10.  
  11. <name>media</name>
  12. <description>Demo project for Spring Boot</description>
  13.  
  14. <parent>
  15. <groupId>org.springframework.boot</groupId>
  16. <artifactId>spring-boot-starter-parent</artifactId>
  17. <version>1.4.0.RELEASE</version>
  18. <relativePath/> <!-- lookup parent from repository -->
  19. </parent>
  20.  
  21. <properties>
  22. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  23. <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  24. <java.version>1.8</java.version>
  25. <mybatis.version>3.2.2</mybatis.version>
  26. </properties>
  27.  
  28. <dependencies>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-web</artifactId>
  32. </dependency>
  33. <dependency>
  34. <groupId>mysql</groupId>
  35. <artifactId>mysql-connector-java</artifactId>
  36. </dependency>
  37. <!-- mybatis -->
  38. <dependency>
  39. <groupId>org.mybatis</groupId>
  40. <artifactId>mybatis</artifactId>
  41. <version>${mybatis.version}</version>
  42. </dependency>
  43. <dependency>
  44. <groupId>org.mybatis</groupId>
  45. <artifactId>mybatis-spring</artifactId>
  46. <version>1.2.0</version>
  47. </dependency>
  48.  
  49. <dependency>
  50. <groupId>org.springframework.boot</groupId>
  51. <artifactId>spring-boot-starter</artifactId>
  52. </dependency>
  53. <dependency>
  54. <groupId>org.springframework.data</groupId>
  55. <artifactId>spring-data-commons</artifactId>
  56. </dependency>
  57. <!-- connection pool -->
  58. <dependency>
  59. <groupId>org.springframework</groupId>
  60. <artifactId>spring-jdbc</artifactId>
  61. </dependency>
  62. <dependency>
  63. <groupId>org.apache.tomcat</groupId>
  64. <artifactId>tomcat-jdbc</artifactId>
  65. </dependency>
  66. <dependency>
  67. <groupId>mysql</groupId>
  68. <artifactId>mysql-connector-java</artifactId>
  69. </dependency>
  70. <dependency>
  71. <groupId>com.fasterxml.jackson.core</groupId>
  72. <artifactId>jackson-databind</artifactId>
  73. <version>2.7.0</version>
  74. </dependency>
  75. <dependency>
  76. <groupId>net.sf.json-lib</groupId>
  77. <artifactId>json-lib</artifactId>
  78. <version>2.4</version>
  79. <classifier>jdk15</classifier>
  80. </dependency>
  81. <dependency>
  82. <groupId>com.fasterxml.jackson.module</groupId>
  83. <artifactId>jackson-module-jaxb-annotations</artifactId>
  84. <version>2.7.0</version>
  85. </dependency>
  86. <dependency>
  87. <groupId>net.sf.json-lib</groupId>
  88. <artifactId>json-lib</artifactId>
  89. <version>2.4</version>
  90. <classifier>jdk15</classifier>
  91. </dependency>
  92. <dependency>
  93. <groupId>dom4j</groupId>
  94. <artifactId>dom4j</artifactId>
  95. <version>1.1</version>
  96. </dependency>
  97. <dependency>
  98. <groupId>org.apache.commons</groupId>
  99. <artifactId>commons-lang3</artifactId>
  100. <version>3.1</version>
  101. </dependency>
  102. <dependency>
  103. <groupId>org.codehaus.jackson</groupId>
  104. <artifactId>jackson-mapper-asl</artifactId>
  105. <version>1.9.13</version>
  106. </dependency>
  107. <dependency>
  108. <groupId>org.codehaus.jackson</groupId>
  109. <artifactId>jackson-core-asl</artifactId>
  110. <version>1.9.13</version>
  111. </dependency>
  112. </dependencies>
  113.  
  114. <build>
  115. <plugins>
  116. <plugin>
  117. <groupId>org.springframework.boot</groupId>
  118. <artifactId>spring-boot-maven-plugin</artifactId>
  119. </plugin>
  120. </plugins>
  121. </build>
  122.  
  123. </project>

2.通过MyBatis-generate自动生成实体类和mapper

3.配置XML文件

在resource下面新建applicationContext.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:p="http://www.springframework.org/schema/p"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:context="http://www.springframework.org/schema/context"
  7. xmlns:jee="http://www.springframework.org/schema/jee"
  8. xmlns:tx="http://www.springframework.org/schema/tx"
  9. xsi:schemaLocation="
  10. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
  11. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
  12. http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
  13. http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
  14. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
  15. <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
  16. <!-- Connection Info -->
  17. <property name="driverClassName" value="${jdbc.driver}" />
  18. <property name="url" value="${jdbc.url}" />
  19. <property name="username" value="${jdbc.username}" />
  20. <property name="password" value="${jdbc.password}" />
  21.  
  22. <!-- Connection Pooling Info -->
  23. <property name="maxActive" value="10" />
  24. <property name="maxIdle" value="50" />
  25. <property name="minIdle" value="0" />
  26. <property name="defaultAutoCommit" value="false" />
  27. </bean>
  28. <!-- MyBatis配置 -->
  29. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  30. <property name="dataSource" ref="dataSource" />
  31. <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
  32. <property name="typeAliasesPackage" value="com.hxyz.media.entity" />
  33. <!-- 显式指定Mapper文件位置 -->
  34. <property name="mapperLocations" value="classpath:/mapper/*Mapper.xml" />
  35. </bean>
  36. <!-- 扫描basePackage下所有以@MyBatisRepository标识的 接口-->
  37. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  38. <property name="basePackage" value="com.hxyz.media.mapper" />
  39. </bean>
  40. </beans>

在resource路径下新建application.properties

  1. server.port=8089
  2. jdbc.driver=com.mysql.jdbc.Driver
  3. jdbc.url=jdbc:mysql://localhost:3306/
  4. jdbc.username=jack
  5. jdbc.password=123456

至此,我们的xml文件已经配置好了,我们可以写一个简单的控制器来测试下数据库是否已经连通

4.测试是否连通

  1. public interface FactoryService {
  2.  
  3. public List<FactoryEntity> findFactory();
  4.  
  5. }
  1. @Service
  2. public class FactoryServiceImpl implements FactoryService{
  3. @Resource
  4. private FactoryEntityMapper factoryMaaper;
  5.  
  6. @Override
  7. public List<FactoryEntity> findFactory() {
  8. FactoryEntityExample example=new FactoryEntityExample();
  9. Criteria createCriteria = example.createCriteria();
  10. List<FactoryEntity> selectByExample = factoryMaaper.selectByExample(example);
  11. return selectByExample;
  12. }
  13.  
  14. }
  1. @RestController
  2. @RequestMapping(value="front/factory")
  3. public class FactoryController {
  4. @Resource
  5. private FactoryService factoryService;
  6.  
  7. @RequestMapping(value="list")
  8. @ResponseBody
  9. public ResponseVo queryAll(){
  10. ResponseVo responseVo=new ResponseVo();
  11. List<FactoryEntity> factoryEntityList = factoryService.findFactory();
  12. responseVo.setData(factoryEntityList);
  13. return responseVo;
  14.  
  15. }
  16.  
  17. }

所以,我们猜一下,现在我请求下,数据会正常的返回么

我们现在请求下

http://localhost:8089/front/factory/list

返回的是

  1. Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.hxyz.media.mapper.FactoryEntityMapper] found for dependency [com.hxyz.media.mapper.FactoryEntityMapper]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@javax.annotation.Resource(shareable=true, lookup=, name=, description=, authenticationType=CONTAINER, type=class java.lang.Object, mappedName=)}
  2. at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1406) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
  3. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1057) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
  4. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1019) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
  5. at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:518) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
  6. at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:496) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
  7. at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:627) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
  8. at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
  9. at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88) ~[spring-beans-4.3.2.RELEASE.jar:4.3.2.RELEASE]
  10. at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:318) ~[spring-context-4.3.2.RELEASE.jar:4.3.2.RELEASE]
  11. ... 34 common frames omitted

没有找到FactoryEntityMapper,但是我们明明是在xml中配置了mapperScannerConfigue

这是因为springboot在启动后并不会主动的去读取xml文件,只会根据类文件间的依赖去加载spring的bean类,所以springboot并没有发现xml中的配置

我们可以在启动类中添加

  1. @ImportResource(locations = "classpath*:/applicationContext.xml")

完整的启动类如下

  1. @ImportResource(locations = "classpath*:/applicationContext.xml")
  2. @SpringBootApplication
  3. //@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class})
  4. public class MediaApplication {
  5.  
  6. public static void main(String[] args) {
  7. SpringApplication.run(MediaApplication.class, args);
  8. }
  9. }

这时我们再启动就可以返回数据了

 

SpringBoot填坑系列---XML方式配置数据库的更多相关文章

  1. MySQL填坑系列--Linux平台下MySQL区分大小写问题

    大家好,我是软件大盗(道),下面开始我们的<MySQL填坑系列>. 笔者最近又在MySQL的边缘试探,然后,试探着,试探着就报错了. 书接上文,系统连接数据库时报错:找不到DB_TIMIN ...

  2. Spring学习(五)bean装配详解之 【XML方式配置】

    一.配置Bean的方式及选择 配置方式 在 XML 文件中显式配置 在 Java 的接口和类中实现配置 隐式 Bean 的发现机制和自动装配原则 方式选择的原则 最优先:通过隐式 Bean 的发现机制 ...

  3. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  4. spring学习笔记 星球日one - xml方式配置bean

    ide: idea lib包的导入:http://webcache.googleusercontent.com/search?q=cache:http://zyjustin9.iteye.com/bl ...

  5. 填坑系列:通过ESXi来配置IPMI

    近日西安的天气很不错,可是看到从其他地方迁移来的主机在新环境下无法远程调试怪郁闷的,这就需要填坑,要不就会给后来者挖更大的坑. 今天遇到的坑是在IPMI的网络设置里面启用了VLAN标签之后,在新环境下 ...

  6. Android通过xml文件配置数据库

    之前一段时间自己封装了两个数据库,一个是ORM数据库,另一个是事件流数据库,项目相应的地址如下: ORM数据库:https://github.com/wenjiang/SimpleAndroidORM ...

  7. 在java的xml文件配置数据库URL地址时提示The reference to entity "characterEncoding" must end with the ';' delimiter.错误信息

    配置数据库的URL<property name="url" value="jdbc:mysql://127.0.0.1:3306/micro_message&quo ...

  8. XML方式配置切面

    1. 概述  一个切面中需要包含什么,才能够作用到连接点?切面中是包含通知的,通知作用到连接点需要有切入点表达式. 除了使用AspectJ注解声明切面,Spring也支持在bean配置文件中声明切面. ...

  9. Quartz.Net系列(十六):通过Plugins模式使用Xml方式配置Job和Trigger和自定义LogPrivider

    1.简单介绍 Quarz.Net中采用插件式来实现配置文件配置,通过XMLSchedulingDataProcessor类进行Xml数据处理 默认配置文件命名:quart_jobs.xml publi ...

随机推荐

  1. 有关ArrayList常用方法的源码解析

    我相信几乎所有的同学在大大小小的笔试.面试过程中都会被问及ArrayList与LinkedList之间的异同点.稍有准备的人这些问题早已烂熟于心,前者基于数组实现,后者基于链表实现:前者随机方法速度快 ...

  2. indexOf和lastIndexOf的使用

    indexOf()和 lastIndexOf()是返回位置index的两个方法:都是接收两个参数,其中,indexOf()方法从数组的开头(位 置 0)开始向后查找:lastIndexOf()方法则从 ...

  3. 门控开关项目--整流桥分析,LED限流电阻选择

    完整的原理图 常见电阻 常见的精度分为5% 和1%精度,碳膜电阻5%精度,金属膜电阻1%精度. 常见的阻值有 10R, 100R, 330R, 1K, 2K, 3K, 5.1K, 10K, 15K, ...

  4. Http异步发送之HttpWebRequest的BeginGetResponse

    关于http异步发送,一开始我的做法都是用thread或者task去完成的:后来发现HttpWebRequest本身就提供一个异步的方法. 总感觉.Net自己提供的异步方法可能要优于我们自己用线程去实 ...

  5. Navigation Controller 创建方法

    添加Navigation Controller的方法主要有两种: 第一种:主要是通过在storyboard中拖入Object library 中的Navigation Controller 第二种方法 ...

  6. 自动化测试—monkeyrunner

    步骤:     1. 在 pycharm 中编写一个 python的脚本,注意:在运行脚本时不要有注释,不然会报错                 2. 在 dos 窗口中运行脚本.         ...

  7. HTML Element 与 Node 的区别

    Element 与 Node 的区别 <html> <head><title>Element & Node</title></head&g ...

  8. gulp总结

    安装 1.安装node 检测是否安装成功的方法,在命令行输入: node -v npm -v 显示版本号则安装成功 2.装cnmp一定要网络好一点 npm install cnpm -g --regi ...

  9. 图片预加载之模拟img.load()

    function imgBatchLoad(){ var instance = this; this.loadCount = 0; this.images = []; this.imgCount = ...

  10. 通过添加filter过滤器 彻底解决ajax 跨域问题

    1.在web.xml添加filter <filter> <filter-name>contextfilter</filter-name> <filter-cl ...