博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SpringBoot填坑系列---XML方式配置数据库
阅读量:4660 次
发布时间:2019-06-09

本文共 8947 字,大约阅读时间需要 29 分钟。

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

1.POM文件

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

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

3.配置XML文件

在resource下面新建applicationContext.xml

在resource路径下新建application.properties

1 server.port=80892 jdbc.driver=com.mysql.jdbc.Driver3 jdbc.url=jdbc:mysql://localhost:3306/4 jdbc.username=jack5 jdbc.password=123456

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

4.测试是否连通

1 public interface FactoryService {2     3     public List
findFactory();4 5 }
@Servicepublic class FactoryServiceImpl implements FactoryService{    @Resource    private FactoryEntityMapper factoryMaaper;    @Override    public List
findFactory() { FactoryEntityExample example=new FactoryEntityExample(); Criteria createCriteria = example.createCriteria(); List
selectByExample = factoryMaaper.selectByExample(example); return selectByExample; }}
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
factoryEntityList = factoryService.findFactory();12 responseVo.setData(factoryEntityList);13 return responseVo;14 15 }16 17 18 }

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

我们现在请求下

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中的配置

我们可以在启动类中添加

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

完整的启动类如下

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

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

 

 

转载于:https://www.cnblogs.com/xmzJava/p/7076460.html

你可能感兴趣的文章
【SAS ADVANCE】Performing Queries Using PROC SQL
查看>>
Hive新功能 Cube, Rollup介绍
查看>>
webpack:(模块打包机)
查看>>
程序员不得不知的座右铭(世界篇)
查看>>
表格-鼠标经过单元格变色(暂不支持IE6)
查看>>
【每日一学】pandas_透视表函数&交叉表函数
查看>>
实时读取日志文件
查看>>
【寒假集训系列2.12】
查看>>
2018牛客多校第六场 I.Team Rocket
查看>>
Vuex了解
查看>>
c++初始化函数列表
查看>>
JS的this总结(上)-call()和apply()
查看>>
BZOJ 1984: 月下“毛景树”( 树链剖分 )
查看>>
Properties类、序列化流与反序列化流
查看>>
Swift学习笔记一:与OC的区别
查看>>
七牛容器实操
查看>>
理解 YOLO
查看>>
检查Linux文件变更Shell脚本
查看>>
ActiveMQ中JMS的可靠性机制
查看>>
oracle操作字符串:拼接、替换、截取、查找
查看>>