问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501

springboot怎么配置数据库

发布网友 发布时间:2022-05-02 02:02

我来回答

2个回答

懂视网 时间:2022-05-02 06:24

<?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 124 </project>

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

技术分享

3.配置XML文件

在resource下面新建applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:aop="http://www.springframework.org/schema/aop" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:jee="http://www.springframework.org/schema/jee" 
 xmlns:tx="http://www.springframework.org/schema/tx" 
 xsi:schemaLocation=" 
 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd 
 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd 
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd 
 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd 
 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"> 
 <bean id="dataSource" class="org.apache.tomcat.jdbc.pool.DataSource" destroy-method="close">
 <!-- Connection Info -->
 <property name="driverClassName" value="${jdbc.driver}" />
 <property name="url" value="${jdbc.url}" />
 <property name="username" value="${jdbc.username}" />
 <property name="password" value="${jdbc.password}" />

 <!-- Connection Pooling Info -->
 <property name="maxActive" value="10" />
 <property name="maxIdle" value="50" />
 <property name="minIdle" value="0" />
 <property name="defaultAutoCommit" value="false" />
 </bean>
 <!-- MyBatis配置 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
 <property name="dataSource" ref="dataSource" />
 <!-- 自动扫描entity目录, 省掉Configuration.xml里的手工配置 -->
 <property name="typeAliasesPackage" value="com.hxyz.media.entity" />
 <!-- 显式指定Mapper文件位置 -->
 <property name="mapperLocations" value="classpath:/mapper/*Mapper.xml" />
 </bean>
 <!-- 扫描basePackage下所有以@MyBatisRepository标识的 接口-->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <property name="basePackage" value="com.hxyz.media.mapper" />
 </bean>
</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 }
@Service
public class FactoryServiceImpl implements FactoryService{
 @Resource
 private FactoryEntityMapper factoryMaaper;

 @Override
 public List<FactoryEntity> findFactory() {
 FactoryEntityExample example=new FactoryEntityExample();
 Criteria createCriteria = example.createCriteria();
 List<FactoryEntity> 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<FactoryEntity> 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 @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方式配置数据库

标签:tco   default   mysq   rom   sql   jdb   osi   连通   driver   

热心网友 时间:2022-05-02 03:32

新建Spring Boot项目,依赖选择JPA(spring-boot-starter-data-jpa)和Web(spring-bootstarter-web)。

配置基本属性 在application.properties里配置数据源和jpa的相关属性
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/springboot
spring.datasource.username=root
spring.datasource.password=123456
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent_output=true

定义映射实体类

定义Controller类
@RestControllerpublic class PersonCtroller {
@Autowired PersonServer personServer;
@RequestMapping("/rollback")
public Person rollback(Person person){
return personServer.savePersonWithRollBack(person);
}
@RequestMapping("/norollback")
public Person noRollback(Person person){
return personServer.savePersonWithOutRollBack(person);
}
}
定义数据访问层
public interface PersonRepository extends JpaRepository<Person, Long> {}
定义Server层
@Servicepublic class PersonServerImp implements PersonServer {
@Autowired
PersonRepository personRepository;
@Transactional(rollbackFor = {IllegalArgumentException.class})
@Override
public Person savePersonWithRollBack(Person person) {
Person p = personRepository.save(person);
if (p.getName().equals("xxx")){
throw new IllegalArgumentException("用户已存在,数据会回滚");
}
return p;
}
}
声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com
不想要淘宝原来的帐号,怎么办,可以注销吗~谢谢了,大神帮忙啊 蓝宝石HD6770显卡求鉴定,GPUZ检测数据如下: 健身60公斤,176身高的人,比较瘦,是不是即便经常锻炼,力气也不一定比胳膊... 农村土地什么情况不予发证?如何解决? 共工治水在前还是怒触不周山在前? 共工触山的故事 有什么类似漂流瓶的软件推荐 漂流瓶软件推荐 保险柜密码怎么改 保险柜密码正确但是打不开怎么办 西安水多少钱一顿 西安哪里可以买自来水 网络电视台是什么东西? Km632无线键盘如何对码? 无线键盘的对码是干嘛的? KM4905无线键鼠 鼠标能用键盘不能用 联想km4905无线键盘怎么用 帮我写出下列词语的近义词非常 &#47;()激动 ()惊奇()紧张()亲手()得意() 晚上睡觉梦见坐车把衣服丢了 中华人民共和国民法典规定的非法人组织包括 惊叹的近义词是什么 个人独资企业就是个体工商户吗 惊诧的近义词是什么 个人独资企业是否属于夫妻共同财产? 个人独资企业法人变更后原法人有责任吗 \请问惊讶的近义词是什么 在淘宝买东西关于拆包验货的问题 惊讶的近义词是惊吓吗? 淘宝,拆包有礼,宝箱解锁,小铺抢人,好礼等你拆,投票测默契,淘拼拼,这些活动创建的流量是从哪里来的 淘宝向小二要拆包视频怎么留言 手机卡欠费不交,自动销号后会影响个人的信誉度,具体表现在哪些方面? 请问淘宝莹销推广,折包有礼怎么弄 不同牌子无线键盘和鼠标的对码问题 键盘对码和不对码有什么区别? 梦见坐车回家丢了钱和东西 N201鼠标如何对码?属于KM4901A的无线键鼠套装 中央电视台的网络网址是多少 vivo手机上的感应锁屏要怎样关闭? 什么是低倍镜,什么是高倍镜 vivo手机上的感应锁屏要怎样关闭 距离感应锁屏怎么用? 怎样区分低倍镜,高倍镜,油镜 u880距离感应锁屏怎么用? 高中生物中,低倍镜和高倍镜的区别是什么呢? 苹果7近距感应锁屏怎么设置? 苹果7近距感应锁屏怎么设置 OPPOace手机有感应锁屏吗? 教授,低倍镜与高倍镜的区别是什么?若是高倍镜放的倍数较大,那为什么在观察装片时,不直接用高倍镜呢 小米note可以触摸感应锁屏吗 怎样区分低倍镜,高倍镜和油镜 越狱插件ios6.1.3感应锁屏 努比亚这个手掌熄屏是什么原理