本次只是简单的运用SpringBoot搭建框架,对其原理并不做深入的探究
1.POM文件
1 24 4.0.0 5 6com.hxyz 7media 80.0.1-SNAPSHOT 9jar 10 11media 12Demo project for Spring Boot 13 1415 20 21org.springframework.boot 16spring-boot-starter-parent 171.4.0.RELEASE 1819 22 27 28UTF-8 23UTF-8 241.8 253.2.2 2629 113 11430 33org.springframework.boot 31spring-boot-starter-web 3234 37 38mysql 35mysql-connector-java 3639 43org.mybatis 40mybatis 41${mybatis.version} 4244 48 49org.mybatis 45mybatis-spring 461.2.0 4750 53org.springframework.boot 51spring-boot-starter 5254 57 58org.springframework.data 55spring-data-commons 5659 62org.springframework 60spring-jdbc 6163 66org.apache.tomcat 64tomcat-jdbc 6567 70mysql 68mysql-connector-java 6971 75com.fasterxml.jackson.core 72jackson-databind 732.7.0 7476 81net.sf.json-lib 77json-lib 782.4 79jdk15 8082 86com.fasterxml.jackson.module 83jackson-module-jaxb-annotations 842.7.0 8587 92net.sf.json-lib 88json-lib 892.4 90jdk15 9193 97dom4j 94dom4j 951.1 9698 102org.apache.commons 99commons-lang3 1003.1 101103 107org.codehaus.jackson 104jackson-mapper-asl 1051.9.13 106108 112org.codehaus.jackson 109jackson-core-asl 1101.9.13 111115 122 123 124116 121117 120org.springframework.boot 118spring-boot-maven-plugin 119
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 ListfindFactory();4 5 }
@Servicepublic class FactoryServiceImpl implements FactoryService{ @Resource private FactoryEntityMapper factoryMaaper; @Override public ListfindFactory() { 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 ListfactoryEntityList = 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 }
这时我们再启动就可以返回数据了