文章目录
- set方法使用xml让spring创建bean
- JdbcTemplate
- spring产生JdbcTemplate对象
- 基于XML的事务控制
- 基于注解的事务控制
- spring获取ApplicationContext
- spring mvc简单配置
自定义的bean用注解,非自定义的bean用xml配置文件
set方法使用xml让spring创建bean
代码语言:javascript复制xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"
<context:property-placeholder location="jdbc.properties"/>
<!--数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
</bean>
<!--jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
// 把容器中的ref=jdbcTemplate通过class=AccountDaoImpl内部的一个name=setJdbcTemplate方法注入给AccountDaoImpl
// 目标bean是class=com.sxf.Dao.Impl.AccountDaoImpl
// 注入方法是name=jdbcTemplate中的setJdbcTemplate
// 注入内容是ref=jdbcTemplate,对应上面的id=jdbcTemplate这个bean
<bean id="accountDao" class="com.sxf.Dao.Impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
代码语言:javascript复制public class AccountDaoImpl implements AccountDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
public class AccountServiceImpl implements AccountService {
private AccountDao accountDao;
public void setAccountDao(AccountDao accountDao){
this.accountDao = accountDao;
}
}
// 从spring容器获取bean
public static void main(String[] args) {
ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
AccountService accountService = app.getBean(AccountService.class);
accountService.transfer();
}
xml
的bean
中,id
是唯一标识符,可以任意设置;class
是要自动创建的类的路径;property
是增加属性设置;name
是set
方法后面的内容(如setJdbcTemplate
中的jdbcTemplate
);ref
是当前xml
中已有的bean
的id
名(对象的引用用ref
)。AccountDaoImpl
中,把容器中的ref=jdbcTemplate
通过class=AccountDaoImpl
内部的一个name=setJdbcTemplate
方法注入给AccountDaoImpl
;
JdbcTemplate
代码语言:javascript复制<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<!-- 事务 -->
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
</dependencies>
代码语言:javascript复制// 创建数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
dataSource.setUser("root");
dataSource.setPassword("1061700625");
JdbcTemplate jdbcTemplate = new JdbcTemplate();
// 设置数据源对象
jdbcTemplate.setDataSource(dataSource);
jdbcTemplate.update("insert into account values(?,?)", "tom", 5000);
spring产生JdbcTemplate对象
代码语言:javascript复制jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.user=root
jdbc.pwd=1061700625
代码语言:javascript复制<context:property-placeholder location="jdbc.properties"/>
<!--数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
</bean>
<!--jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
代码语言:javascript复制ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
JdbcTemplate jdbcTemplate= app.getBean(JdbcTemplate.class);
int row = jdbcTemplate.update("insert into account values(?,?)", "ros", 5000);
System.out.println(row);
或
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTempTest {
@Test
public void test2() {
int row = jdbcTemplate.update("insert into account values(?,?)", "bob", 5000);
System.out.println(row);
}
}
// 查询所有
List<Account> query = jdbcTemplate.query("select * from account;", new BeanPropertyRowMapper<Account>(Account.class));
System.out.println(query);
// 查询单个
Account query2 = jdbcTemplate.queryForObject("select * from account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom");
System.out.println(query2);
//聚合查询
Long aLong = jdbcTemplate.queryForObject("select count(*) from account", Long.class);
System.out.println(aLong);
基于XML的事务控制
切点:被增强的方法(业务方法) 通知:事务控制 切面:配置织入
配置要点:
- 平台事务管理器配置
- 事务通知配置
- 事务aop织入配置
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
<context:property-placeholder location="jdbc.properties"/>
<!--数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
</bean>
<!--jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="accountDao" class="com.sxf.Dao.Impl.AccountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"/>
</bean>
<!--业务层,目标对象,内部的方法就是切点-->
<bean id="accountService" class="com.sxf.Service.Impl.AccountServiceImpl">
<property name="accountDao" ref="accountDao"/>
</bean>
<!--指定平台事务管理器,jdbc和mybatis使用DataSourceTransactionManager-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--内部控制事务时,需要通过DataSource.getConnection-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--通知,事务的增强-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!--设置事务的属性信息-->
<tx:attributes>
<!-- 哪些方法被增强-->
<tx:method name="transfer" read-only="false"/>
<tx:method name="update*" timeout="20"/>
<!-- 所有方法-->
<tx:method name="*" timeout="10"/>
</tx:attributes>
</tx:advice>
<!--配置事务的AOP织入-->
<aop:config>
<!--spring为事务增强的提供配置,一个通知的切面;普通用aop:aspect-->
<aop:advisor advice-ref="txAdvice" pointcut="execution(* com.sxf.Service.Impl.*.*(..))"></aop:advisor>
</aop:config>
代码语言:javascript复制public void out(String name, Double out) {
jdbcTemplate.update("update account set money=money-? where name=?", out, name);
}
@Override
public void in(String name, Double in) {
jdbcTemplate.update("update account set money=money ? where name=?", in, name);
}
public void transfer() {
accountDao.out("tom", 100.0);
int a = 1/0; // 故意抛出异常
accountDao.in("ros", 100.0);
}
基于注解的事务控制
代码语言:javascript复制<context:property-placeholder location="jdbc.properties"/>
<!--组件扫描,扫com.sxf这个包-->
<context:component-scan base-package="com.sxf"/>
<!--数据源对象-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="user" value="${jdbc.user}"/>
<property name="password" value="${jdbc.pwd}"/>
</bean>
<!--jdbc模板对象-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--指定平台事务管理器,jdbc和mybatis使用DataSourceTransactionManager-->
<beanid="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--内部控制事务时,需要通过DataSource.getConnection-->
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事务的注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager"/>
代码语言:javascript复制@Service("accountService")
public class AccountServiceImpl implements AccountService {
@Autowired
private AccountDao accountDao;
@Override
@Transactional
public void transfer() {
accountDao.out("tom", 100.0);
int a = 1/0; // 故意抛出异常
accountDao.in("ros", 100.0);
}
}
spring获取ApplicationContext
pom.xml
代码语言:javascript复制<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
web.xml
代码语言:javascript复制<!--设置全局变量-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--配置spring内置的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
applicationContext.xml
代码语言:javascript复制<bean id="userDao" class="com.sxf.dao.impl.UserDaoImpl"> </bean>
<bean id="userService" class="com.sxf.service.impl.UserServiceImpl">
<property name="userDao" ref="userDao"/>
</bean>
UserServlet.java
代码语言:javascript复制@WebServlet("/")
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) {
ServletContext servletContext = req.getServletContext();
WebApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
UserServiceImpl userService = app.getBean(UserServiceImpl.class);
userService.save();
}
}
spring mvc简单配置
导入springmvc相关坐标 pom.xml
代码语言:javascript复制<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
配置springmvc核心控制器DispatcherServlet web.xml
代码语言:javascript复制<!--配置springmvc前端控制器-->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
spring-mvc.xml
代码语言:javascript复制<!--controller的组件扫描-->
<context:component-scan base-package="com.sxf.controller"/>
创建controller类,使用注解配置controller类中业务方法的映射地址 UserController.java
代码语言:javascript复制@Controller
public class UserController {
@RequestMapping("/save")
public String save() {
System.out.println("save");
return "ok.jsp";
}
}
创建视图页面 webapp/ok.jsp
代码语言:javascript复制<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
ok save
</body>
</html>