In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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)
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.