Author: Yuge WeChat/QQ: Warmheart170712 / 939638113
Sorting is not easy, but sharing is important. Welcome to forward and collect, help the interview ashore, learn programming and find brother Yu
Course content introduction:
1. Introduction to Spring framework
2. IOC container
2.1 underlying principle of IOC
2.2. IOC interface - BeanFactory
2.3. IOC operation bean management
2.4. There are two ways to manage Bean operations
2.4.1. IOC operation bean management - based on xml (demo01, demo02)
2.4.2. IOC operation bean management - annotation based method (demo03 demo04)
3,AOP
3.1. What is AOP (concept)
3.2 AOP underlying principle
3.3 AOP related terms
3.4 AOP operation preparation
3.5. AOP operation - based on AspectJ annotation
3.6, AOP operation - based on AspectJ configuration file
4,JdbcTemplate
4.1. What is a JdbcTemplate
4.2 preparation
4.3. CRUD based on the preparation of 4.2
5. Transaction management
What is a business
Transaction is the most basic unit of database operation and a logical group of operations,
Either all succeed. If one fails, all operations fail
Typical scene. bank transfer
5.2 four characteristics of transaction ACID
Atomicity: atomicity means that transactions in the database are executed as atomicity. That is, it cannot be divided again. The whole statement is either executed or not executed
Consistency: consistency means that the integrity constraints of the database are not destroyed before and after the transaction.
Isolation: the execution of transactions does not interfere with each other. It is impossible for a transaction to see the data at a certain time when other transactions are running
Persistence: it means that after the transaction is completed, the changes made by the transaction to the database will be permanently saved in the database and will not be rolled back
5.3. Create a new transaction operating environment
Step 1: add records for creating database and table: stay user_db Create in Library t_account Step 2: create service,build dao. Complete object creation and injection relationship stay service Medium injection dao,dao Medium injection jdbcTemplate,stay jdbcTemplate Medium injection DataSource Step 3. In dao Create two methods, more money and less money, in service Method of creating transfer in @Repository public class AccountDaoImpl implements AccountDao{ @Autowired private JdbcTemplate jdbcTemplate; @Override public void addMoney() { String sql = "update t_account set money=money+? where username=?"; //Set mary to have 100 more money jdbcTemplate.update(sql, 100,"mary"); } @Override public void reduceMoney() { String sql = "update t_account set money=money-? where username=?"; //The setting makes lucy's money less by 100 jdbcTemplate.update(sql, 100,"lucy"); } Step 4: write test @Test public void test1(){ //Create container to load core configuration file ApplicationContext context = new ClassPathXmlApplicationContext("spring1.xml"); //Get object from container AccountService accountService = context.getBean("accountService", AccountService.class); //Call method accountService.accountMoney(); } Step 5: run to view the database lucy 100 less mary 100 more Step 6: simulate abnormal If there is an exception in the above code public void accountMoney(){ //100 less luncy accountDao.reduceMoney(); //Simulated intention int a = 1/0; //maey more than 100 accountDao.addMoney(); } The results are: lucy 100 less, but mary No increase of 100 Step 7. Use transaction to resolve the exception //Method of transfer public void accountMoney(){ try{ //Step 1: start the transaction //Step 2: process business //100 less luncy accountDao.reduceMoney(); //Simulated intention int a = 1/0; //maey more than 100 accountDao.addMoney(); //Step 3: if no exception occurs, the transaction is committed }catch(Exception e){ //Step 4: if an exception occurs, the transaction is rolled back } }
5.4. Spring transaction management operation
5.4.1. Add transactions to the Service layer in the three-tier architecture of Java EE, that is, the business logic layer
5.4.2. Transaction management in Spring - two methods
Transaction during programming: not recommended, the code is bloated and difficult to maintain Declarative transaction: easy to use
5.4.3 declarative transaction:
1,Annotation based approach 2,be based on xml Profile mode 3,stay Spring Declarative transaction management. The bottom layer is used AOP principle 4,An interface can be provided to represent the transaction manager. This interface provides different implementation classes for different frameworks