欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

Spring jdbc的搭建

发布时间:2025/3/20 javascript 51 豆豆
生活随笔 收集整理的这篇文章主要介绍了 Spring jdbc的搭建 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

为什么80%的码农都做不了架构师?>>>   

 

首先得在pom.xml中新增两个依赖,不然你会发现要用DriverManagerDataSource这个类都没有

<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> </dependency> <dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.43</version></dependency>

然后在controller里写了段并加断点测试一下,与数据库联通了,没毛病

DriverManagerDataSource dataSource = new DriverManagerDataSource();dataSource.setUrl("jdbc:mysql://localhost:3306/gotoxoyo");dataSource.setUsername("root");dataSource.setPassword("go111112");dataSource.setDriverClassName("com.mysql.jdbc.Driver");JdbcCustomerDao customerDao = new JdbcCustomerDao();customerDao.setDataSource(dataSource);Customer customer = new Customer(1, "mkyong",28);customerDao.insert(customer);Customer customer1 = customerDao.findByCustomerId(1);System.out.println(customer1);

现在该把这个数据配置移到xml中了,在src根目录下新建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:context="http://www.springframework.org/schema/context"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd"><bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource"><property name="driverClassName" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/gotoxoyo" /><property name="username" value="root" /><property name="password" value="go111112" /></bean><bean id="customerDao" class="com.gotoxoyo.demo.dao.JdbcCustomerDao"><property name="dataSource" ref="dataSource" /></bean><!-- <context:property-placeholder location="jdbc.properties"/>--> </beans>

现在还有个问题,controller层如果要用dao类和pojo类,还需要初始化,如下代码:

JdbcCustomerDao customerDao = new JdbcCustomerDao();customerDao.setDataSource(dataSource);Customer customer = new Customer(1, "mkyong",28);customerDao.insert(customer);Customer customer1 = customerDao.findByCustomerId(1);System.out.println(customer1);

能不能直接让spring初始化呢,可以的,在applicationContext.xml中配置

<bean id="customerDao" class="com.gotoxoyo.demo.dao.JdbcCustomerDao"><property name="dataSource" ref="dataSource" /></bean><bean class="com.gotoxoyo.demo.pojo.Customer" id="customer"><property name="custId" value="2" /><property name="name" value="Yang" /><property name="age" value="32" /></bean>

然后controller层的类就只需要两句代码了

@Resourceprivate CustomerDao customerDao;public CustomerDao getCustomerDao() {return customerDao;}public void setCustomerDao(CustomerDao customerDao) {this.customerDao = customerDao;}@Resourceprivate Customer customer;public Customer getCustomer() {return customer;}public void setCustomer(Customer customer) {this.customer = customer;}在方法里只需一句代码customerDao.insert(customer);

 

转载于:https://my.oschina.net/xoyo/blog/1503282

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是生活随笔为你收集整理的Spring jdbc的搭建的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。