bean+properties普通配置方式
jdbc.properties
jdbc.user=root
jdbc.password=root
jdbc.url=jdbc\:mysql\://localhost\:3306/springdb
jdbc.driver=com.mysql.jdbc.Driver
applicationContext.xml
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/jdbc.properties"></property>
</bean>
<!--方法一 直接连接 -->
<beanid="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
Test
@Test
public void test01() throws SQLException{
//测试数据库连接是否正常
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
DataSource dataSource = (DataSource) context.getBean("dataSource");
boolean bean = dataSource.getConnection().isClosed();
System.out.println(bean);
Connection conn = dataSource.getConnection();
Statement statement = conn.createStatement();
String sql = "select * from user ";
ResultSet resultSet = statement.executeQuery(sql);
while(resultSet.next()){
int id = resultSet.getInt("id");
String name = resultSet.getString("name");
System.out.println(id+":"+name);
}
resultSet.close();
statement.close();
conn.close();
}
bean+properties+spring模板的c3p0方式
jdbc.user=root
jdbc.password=root
jdbc.url=jdbc\:mysql\://localhost\:3306/springdb
jdbc.driver=com.mysql.jdbc.Driver
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">
<!-- 扫描包 -->
<context:component-scan base-package="com.peng"></context:component-scan>
<!-- 扫描注解 -->
<context:annotation-config></context:annotation-config>
<!-- aop -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/jdbc_config.properties"></property>
</bean>
<!--c3p0 -->
<bean id="c3p0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="minPoolSize" value="3"></property> <!--最小连接数 -->
<property name="initialPoolSize" value="5"></property> <!-- 初始化连接数 -->
<property name="acquireIncrement" value="3"></property> <!-- 每次增长的个数 -->
</bean>
<!--配置Spring中的jdbcTempate -->
<bean id="jdbcTempate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0dataSource"></property>
</bean>
</beans>
Test
package com.peng.test;
import org.junit.Before;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
public class Test {
private JdbcTemplate jdbcTemplate;
@Before
public void init() {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
jdbcTemplate = (JdbcTemplate) ac.getBean("jdbcTempate");
}
@org.junit.Test
public void JdbcTestInsert() throws Exception {
String sql = "insert into user values(?,?)";
// 增删改操作都是用update
int rows = jdbcTemplate.update(sql, 18, "帅哥");
System.out.println(rows);
}
@org.junit.Test
public void JdbcTestQuery() throws Exception {
String sql = "select * from user";
// 增删改操作都是用update
SqlRowSet queryForRowSet = jdbcTemplate.queryForRowSet(sql);
while (queryForRowSet.next()) {
System.out.println(queryForRowSet.getInt(queryForRowSet
.findColumn("id")));
System.out.println(queryForRowSet.getString(queryForRowSet
.findColumn("name")));
}
}
}
封装对象
RowMapper<Person> rowMapper = new RowMapper<Person>() {
@Override //使用匿名内部类的形式
public Person mapRow(ResultSet rs, int rowNum) throws SQLException {
Person p1 = new Person();
p1.setId(rs.getInt("id"));
p1.setName(rs.getString("name"));
return p1;
}
};
mvc的spring的jdbc
代码
com.peng.dao
UserDao
package com.peng.dao;
import com.peng.pojo.User;
public interface UserDao {
boolean saveUser(User user);
}
UserDaoImpl
package com.peng.dao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.peng.pojo.User;
@Repository(value = "userDao")
public class UserDaoImpl implements UserDao {
@Autowired
@Qualifier(value = "jdbcTempate")
private JdbcTemplate template;
public boolean saveUser(User user) {
int rows = -1;
rows = template.update("insert into user values(?,?)", user.getId(),
user.getName());
return !(rows == -1);
}
}
com.peng.pojo
User
package com.peng.pojo;
public class User {
private int id;
private String name;
public User() {
super();
}
public User(int id, String name) {
super();
this.id = id;
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
com.peng.service
UserService
package com.peng.service;
import com.peng.pojo.User;
public interface UserService {
boolean SavePerson(User user);
}
UserServiceImp l
package com.peng.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.peng.dao.UserDao;
import com.peng.pojo.User;
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier(value = "userDao")
private UserDao userDao;
@Override
public boolean SavePerson(User user) {
return userDao.saveUser(user);
}
}
com.peng.test
Test
package com.peng.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.peng.pojo.User;
import com.peng.web.UserServlet;
public class Test {
@org.junit.Test
public void saveUser() {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"applicationContext.xml");
UserServlet userServlet = (UserServlet) ac.getBean("userServlet");
User user = new User(111, "aaa");
userServlet.saveUser(user);
}
}
com.peng.web
UserServlet
package com.peng.web;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import com.peng.pojo.User;
import com.peng.service.UserService;
@Controller(value = "userServlet")
public class UserServlet {
@Autowired
@Qualifier(value = "userService")
private UserService userService;
public void saveUser(User user) {
if (userService.SavePerson(user)) {
System.out.println("插入成功!");
} else {
System.out.println("插入失败!");
}
}
}
配置文件
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd ">
<!-- 扫描包 -->
<context:component-scan base-package="com.peng"></context:component-scan>
<!-- 扫描注解 -->
<context:annotation-config></context:annotation-config>
<!-- aop -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<!-- 配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/jdbc_config.properties"></property>
</bean>
<!--c3p0 -->
<bean id="c3p0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="minPoolSize" value="3"></property> <!--最小连接数 -->
<property name="initialPoolSize" value="5"></property> <!-- 初始化连接数 -->
<property name="acquireIncrement" value="3"></property> <!-- 每次增长的个数 -->
</bean>
<!--配置Spring中的jdbcTempate -->
<bean id="jdbcTempate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0dataSource"></property>
</bean>
</beans>
jdbc_config.properties
jdbc.user=root
jdbc.password=root
jdbc.url=jdbc\:mysql\://localhost\:3306/springdb
jdbc.driver=com.mysql.jdbc.Driver
配置文件形式(关键代码)
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 扫描包 -->
<context:component-scan base-package="com.peng"></context:component-scan>
<!-- 扫描注解 -->
<context:annotation-config></context:annotation-config>
<!-- aop之事务 -->
<aop:config>
<aop:pointcut expression="within(com.peng.service.*)" id="pc" />
<!-- advisor:顾问 -->
<aop:advisor advice-ref="transactionAdvisor" pointcut-ref="pc" />
</aop:config>
<!-- 配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/jdbc_config.properties"></property>
</bean>
<!--c3p0 -->
<bean id="c3p0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="minPoolSize" value="3"></property> <!--最小连接数 -->
<property name="initialPoolSize" value="5"></property> <!-- 初始化连接数 -->
<property name="acquireIncrement" value="3"></property> <!-- 每次增长的个数 -->
</bean>
<!--配置Spring中的jdbcTempate -->
<bean id="jdbcTempate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0dataSource"></property>
</bean>
<!-- 事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0dataSource"></property>
</bean>
<!--配置事物通知 他是一个环绕通知,类名为TransactionInterceptor transaction-manager="transactionManager"可以省略这个配置 -->
<tx:advice id="transactionAdvisor" transaction-manager="transactionManager">
<tx:attributes>
<!--name方法名 propagation:事务的传播属性 REQUIRED 必须添加事务(如果当前有事务,则用当前的;如果没有,则新建事务)
SUPPORTS支付事务(有事务则用当前事务,没有的话也不用去创建)read-only ="true" 只读 -->
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="Save*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="delete*" propagation="REQUIRED" />
<tx:method name="find*" propagation="SUPPORTS" read-only="true" />
<tx:method name="*" propagation="SUPPORTS" read-only="true" />
</tx:attributes>
</tx:advice>
</beans>
测试(service层写个bug)
package com.peng.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.peng.dao.UserDao;
import com.peng.pojo.User;
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier(value = "userDao")
private UserDao userDao;
@Override
public boolean SavePerson(User user) {
userDao.saveUser(new User(666, "66"));
int bug = 1 / 0;
return userDao.saveUser(user);
}
}
注解形式
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 扫描包 -->
<context:component-scan base-package="com.peng"></context:component-scan>
<!-- 扫描注解 -->
<context:annotation-config></context:annotation-config>
<!-- 事务 -->
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 配置文件 -->
<bean
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:/jdbc_config.properties"></property>
</bean>
<!--c3p0 -->
<bean id="c3p0dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"></property>
<property name="jdbcUrl" value="${jdbc.url}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="minPoolSize" value="3"></property> <!--最小连接数 -->
<property name="initialPoolSize" value="5"></property> <!-- 初始化连接数 -->
<property name="acquireIncrement" value="3"></property> <!-- 每次增长的个数 -->
</bean>
<!--配置Spring中的jdbcTempate -->
<bean id="jdbcTempate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="c3p0dataSource"></property>
</bean>
<!-- 事务 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="c3p0dataSource"></property>
</bean>
</beans>
注解添加的位置(service层)
package com.peng.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.peng.dao.UserDao;
import com.peng.pojo.User;
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier(value = "userDao")
private UserDao userDao;
@Override
@Transactional
public boolean SavePerson(User user) {
userDao.saveUser(new User(666, "66"));
int bug = 1 / 0;
System.out.println(bug);
return userDao.saveUser(user);
}
}
事务的回滚策略
默认:运行时异常回滚
package com.peng.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.peng.dao.UserDao;
import com.peng.pojo.User;
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier(value = "userDao")
private UserDao userDao;
@Override
@Transactional//默认无参数
public boolean SavePerson(User user) {
userDao.saveUser(new User(666, "66"));
int bug = 1 / 0;
System.out.println(bug);
return userDao.saveUser(user);
}
}
更改策略:将编译时的错误也进行回滚
package com.peng.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.peng.dao.UserDao;
import com.peng.pojo.User;
@Service(value = "userService")
public class UserServiceImpl implements UserService {
@Autowired
@Qualifier(value = "userDao")
private UserDao userDao;
@Override
@Transactional(rollbackFor = SQLException.class)//编译错误也回滚
public boolean SavePerson(User user) throws SQLException{
userDao.saveUser(new User(666, "66"));
int bug = 1 / 0;
System.out.println(bug);
return userDao.saveUser(user);
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- yrrf.cn 版权所有 赣ICP备2024042794号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务