Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of XML configuration transaction Control in Spring Framework

Shulou Source: shulou.com Published: 2022-06-03 13:35:29 10月02日 Update

This article mainly introduces the example analysis of XML configuration transaction control in Spring framework, which is very detailed and has certain reference value. Friends who are interested must read it!

1. Declarative transaction control based on XML. Environment building

Copy the necessary jar packages to the project's lib directory

two。 Create a configuration file for spring and import constraints

All the XML configurations for this test are given directly here, and the details are described in turn later.

3. Prepare database tables and entity classes

User database table

Create database mybase2;USE mybase2;CREATE TABLE `user` (`id` INT (10) NOT NULL AUTO_INCREMENT COMMENT 'key', `name` VARCHAR (20) NOT NULL DEFAULT '0keys, `age`INT (10) NOT NULL DEFAULT' 0mm, PRIMARY KEY (`id`) COLLATE='utf8_general_ci'ENGINE=InnoDB;INSERT INTO user values (NULL,' Zhou Dongyu', 21); INSERT INTO user values (NULL,' Ma Dongmei', 18); INSERT INTO user values (NULL,' Ma Ximei', 19)

User entity class

Package com.gql.entity;import java.io.Serializable;/** * class description: * User entity class * @ guoqianliang1998. * / public class User implements Serializable {/ * generate serial number * / private static final long serialVersionUID =-4492760954899814333L; private int id; private String name; private int age; 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;} public int getAge () {return age;} public void setAge (int age) {this.age = age;} 4. Business layer Interface and implementation Class

Business layer interface

Package com.gql.service;import java.util.List;import com.gql.entity.User;/** * class description: * Business layer interface * @ guoqianliang1998. * / public interface UserService {void save (User user); void update (int id, User user); void delete (int id); User getUser (int id); List getUserList (int [] ids); List getUserList ();}

Business layer implementation class

Package com.gql.service;import java.util.List;import com.gql.dao.UserDao;import com.gql.entity.User;/** * class description: * Business layer implementation class * @ guoqianliang1998. * / public class UserServiceImp implements UserService {private UserDao userDao; public void setUserDao (UserDao userDao) {this.userDao = userDao;} @ Override public void save (User user) {userDao.save (user); int I = 1and0 } @ Override public void update (int id, User user) {userDao.update (id, user);} @ Override public void delete (int id) {userDao.delete (id);} @ Override public User getUser (int id) {User user = userDao.getUser (id) Return user;} @ Override public List getUserList (int [] ids) {List userList = userDao.getUserList (ids); return userList;} @ Override public List getUserList () {List list = userDao.getUserList (); return list;}} 5. Data access layer Interface and implementation Class

Data access layer interface

Package com.gql.dao;import java.util.List;import com.gql.entity.User;/** * class description: * data access layer interface * @ guoqianliang1998. * / public interface UserDao {void save (User user); void update (int id, User user); void delete (int id); User getUser (int id); List getUserList (int [] ids); List getUserList ();}

Data access layer implementation class

Package com.gql.dao;import java.sql.ResultSet;import java.sql.SQLException;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;import com.gql.RowMapper.UserRowMapper;import com.gql.entity.User / * * Class description: * data access layer implementation class * @ guoqianliang1998. * / public class UserDaoImp implements UserDao {private JdbcTemplate jdbcTemplate; public void setJdbcTemplate (JdbcTemplate jdbcTemplate) {this.jdbcTemplate = jdbcTemplate;} @ Override public void save (User user) {String sql = "INSERT INTO user values (NULL,?,?)"; jdbcTemplate.update (sql, user.getName (), user.getAge ()) } @ Override public void update (int id, User user) {String sql = "update user set name=?,age=? WHERE id =? "; jdbcTemplate.update (sql, user.getName (), user.getAge (), id);} @ Override public void delete (int id) {String sql =" DELETE FROM user WHERE id =?; "; jdbcTemplate.update (sql, id) @ Override public User getUser (int id) {String sql = "select * from user where id =?"; User user = jdbcTemplate.queryForObject (sql, new RowMapper () {@ Override public User mapRow (ResultSet rs, int rowNum) throws SQLException {User user = new User () User.setId (rs.getInt (1)); user.setName (rs.getString (2)); user.setAge (rs.getInt (3)); return user;}}, id) Return user;} @ Override public List getUserList (int [] ids) {String sql = "select * from user where id in (: ids)"; NamedParameterJdbcTemplate n = new NamedParameterJdbcTemplate (jdbcTemplate.getDataSource ()); Map paramMap = new HashMap (); List list = new ArrayList () For (int I = 0; I < ids.length; iDIS +) {list.add (IDS [I]);} paramMap.put ("ids", list) List userList = n.query (sql, paramMap, new RowMapper () {@ Override public User mapRow (ResultSet rs, int rowNum) throws SQLException {User user = new User (); user.setId (rs.getInt (1)) User.setName (rs.getString (2)); user.setAge (rs.getInt (3)); return user;}}); return userList / / the second way of writing is ↓↓↓ / / StringBuilder sb = new StringBuilder (); / / sb.append ("SELECT * FROM user WHERE id =?"); / / for (int item0)

Tags: Transaction configuration data business interface control transaction management entity database management framework example analysis content pointcut container attribute data source article level Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn MySQL Shulou Technology Shulou Tech Info Microsoft