NOTE

2.53 使用

1. 创建项目 2. pom.xml 3. 代码 - DbConfig - TestService - TestDao

Java创建于 更新于 historical

这是历史学习笔记,可能存在过时或不完整的理解。

1. 创建项目

2. pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.zsk</groupId>
    <artifactId>spring-context-test</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.3.24.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.24.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.3.24.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.46</version>
        </dependency>
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
    </dependencies>
</project>

3. 代码

  • DbConfig
@ComponentScan("com.zsk.context.database")
@Configuration
@EnableTransactionManagement
public class DbConfig
{
    @Bean
    public DataSource dataSource() throws PropertyVetoException
    {
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setUser("root");
        dataSource.setPassword("zskroot");
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/tao");
        return dataSource;
    }

    @Bean
    public JdbcTemplate jdbcTemplate() throws PropertyVetoException
    {
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        jdbcTemplate.setDataSource(dataSource());
        return jdbcTemplate;
    }

    @Bean
    public PlatformTransactionManager transactionManager() throws PropertyVetoException
    {
        return new DataSourceTransactionManager(dataSource());
    }

    public static void main(String[] args)
    {
        AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(DbConfig.class);
        TestService testService = applicationContext.getBean(TestService.class);
        testService.insert();
    }
}
  • TestService
@Service
public class TestService
{
    @Autowired
    private TestDao testDao;

    @Transactional
    public void insert()
    {
        testDao.insert();
        int i = 10 / 0;
    }
}
  • TestDao
@Repository
public class TestDao
{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public void insert()
    {
        String sql = "insert into `ttest`(id, status) values (66, 'close')";

        jdbcTemplate.execute(sql);
    }

}