声明式事务
环境搭建
导入相关依赖
配置数据源、JdbcTemplate(Spring提供的简化数据库操作的工具)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| @EnableTransactionManagement @ComponentScan("com.wxs.tx") @Configuration public class TxConfig { @Bean public DataSource dataSource() throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setUser("root"); dataSource.setPassword("abc123aa"); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test"); return dataSource; }
@Bean public JdbcTemplate jdbcTemplate() throws PropertyVetoException { JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource()); return jdbcTemplate; } }
|
给方法标注上 @Transactional
注解
@EnableTransactionManagement
开启基于注解的事务管理功能
配置事务管理器来控制事务
1 2 3 4 5 6
| @Bean public PlatformTransactionManager transactionManager() throws PropertyVetoException { return new DataSourceTransactionManager(dataSource()); }
|
简单测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| @Repository public class UserDao {
@Autowired private JdbcTemplate jdbcTemplate;
@Transactional public void insert() { String sql = "INSERT INTO `tbl_user` (username,age) VALUES (?,?)"; String username = UUID.randomUUID().toString().substring(0, 5); jdbcTemplate.update(sql, username, 20); int num = 1 / 0; } }
|
1 2 3 4 5 6 7 8 9 10 11
| @Service public class UserService {
@Autowired UserDao userDao;
public void insert() { userDao.insert(); System.out.println("插入完成!!!"); } }
|
Servlet 3.0
基于注解的Servlet(原生的servlet用的比较少):
1 2 3 4 5 6 7 8 9
| @WebServlet("/hello") public class HelloServlet extends HttpServlet {
@Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.getWriter().write("Hello!"); } }
|
1 2 3
| <html> <a href="hello"></a> </html>
|
Shared libraries / runtime pluggability
容器在启动应用时,会扫描当前应用每一个jar包里面
META-INF/services/javax.servlet.ServletContainerInitializer
指定的实现类,启动并运行这个实现类的方法
SpringMVC
简单示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
|
public class MyWebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override protected Class<?>[] getRootConfigClasses() { return new Class[]{RootConfig.class}; }
@Override protected Class<?>[] getServletConfigClasses() { return new Class[]{AppConfig.class}; }
@Override protected String[] getServletMappings() {
return new String[]{"/"}; } }
|
1 2 3 4 5 6 7 8 9
|
@ComponentScan(value = "com.wxs",excludeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}) }) public class RootConfig { }
|
1 2 3 4 5 6 7 8 9 10
|
@ComponentScan(value = "com.wxs",includeFilters = { @ComponentScan.Filter(type = FilterType.ANNOTATION,classes = {Controller.class}) },useDefaultFilters = false) public class AppConfig { }
|
1 2 3 4 5 6 7
| @Service public class HelloService {
public String sayHello(String name) { return "hello" + name; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Controller public class HelloController {
@Autowired HelloService helloService;
@RequestMapping("/hello") @ResponseBody public String hello() { helloService.sayHello("wxs"); return "success"; } }
|
定制SpringMVC
@EnableWebMvc
:开启SpringMVC定制配置功能
- 继承类
WebMvcConfigurer