大数据正式32 悠悠 2022-06-03 08:44 148阅读 0赞 # 大数据正式32 # ### Spring中的JDBC ### * jar包准备 * ![zW1gEQQ.png][] * 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\_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: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 * 整体结构 * ![XqnfF5A.png][] * 代码 * 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 ### Spring事务管理 ### * jar包 * ![W795tcL.png][] * 配置文件形式(关键代码) * 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); } } ### 注:servlet和service层中 ### * 事务的回滚 * 当在servlet中的CRUD的操作分属不同的事务,此时的事务是不可以回滚的 * 事务嵌套(涉及到多表,则放在service层 ) ### 补充 ### * ![EDLIA15.jpg][] * ![Iw0fbgZ.jpg][] [zW1gEQQ.png]: https://i.imgur.com/zW1gEQQ.png [XqnfF5A.png]: https://i.imgur.com/XqnfF5A.png [W795tcL.png]: https://i.imgur.com/W795tcL.png [EDLIA15.jpg]: https://i.imgur.com/EDLIA15.jpg [Iw0fbgZ.jpg]: https://i.imgur.com/Iw0fbgZ.jpg
相关 大数据正式5 大数据正式5 常见的shell命令 管道命令 管道符| 将两个命令隔开,左边命令的输出就会作为管道右边命令的输入 连续使 旧城等待,/ 2022年06月06日 10:29/ 0 赞/ 218 阅读
相关 大数据正式2 大数据正式2 用户身份与用户组记录的文件 在Linux系统当中,默认情况下所有的系统上的账号信息都记录在/etc/passwd这个文件内(包括root用户), 快来打我*/ 2022年06月06日 08:38/ 0 赞/ 143 阅读
相关 大数据正式10 大数据正式10 jQuery 定义:jQuery是一个“写的更少”,但“做的更多”的轻量级JavaScript函数库 优势 1. 可 ゞ 浴缸里的玫瑰/ 2022年06月05日 06:24/ 0 赞/ 226 阅读
相关 大数据正式32 大数据正式32 Spring中的JDBC jar包准备 ![zW1gEQQ.png][] bean+properties普通配置 悠悠/ 2022年06月03日 08:44/ 0 赞/ 149 阅读
相关 大数据正式27 大数据正式27 Spring 先来张图简单看一下 ![oQySJMC.png][] spring框架的特点 1 悠悠/ 2022年06月03日 04:38/ 0 赞/ 129 阅读
相关 大数据正式37 大数据正式37 Maven 传统项目存在的弊端 1. 导入jar包得经验丰富 2. 传统项目打包方式不通用,不能很好的支持聚合项 左手的ㄟ右手/ 2022年06月02日 01:46/ 0 赞/ 140 阅读
相关 大数据正式36 大数据正式36 MyBatis的接口形式 注意两点 1. 接口名---namespace值对应 2. 方法名---id一致 淩亂°似流年/ 2022年06月02日 01:12/ 0 赞/ 248 阅读
相关 大数据正式34 大数据正式34 Spring+SpringMVC 小例子 效果图 ![hsIEQmd.png][] 功能说明 川长思鸟来/ 2022年06月02日 00:16/ 0 赞/ 257 阅读
还没有评论,来说两句吧...