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 > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article is to share with you about Spring database programming, the editor thinks it is very practical, so I share it with you to learn. I hope you can get something after reading this article.
Preface
First introduction, the core understanding and then look back at these I think the effect is better, I do not know whether what is right, if the understanding is not correct, but also look for advice. This article for Spring database programming, mainly about jdbcTemplate, let beginners get started directly.
Database cut-in
Database programming is the basis of Internet programming, Spring framework provides developers with JDBC template model, that is, jdbcTemplate, it can simplify a lot of code, need to be reminded that jdbcTemplate is not commonly used in practical applications, but I think for beginners, no matter how small the fly is meat, if you want to move forward, you have to eat these small meat first.
Spring JDBC configuration
To use Spring JDBC to operate the database, you need to configure it as follows
When configuring JDBC templates, you need to inject dataSource into jdbcTemplate, and when you need to use jdbcTemplate in the data access layer (Dao class), you also need to inject jdbcTemplate into the corresponding Bean. I'll demonstrate the simplest annotation injection here.
@ Repository ("userDao") public class UserDaoImpl implements UserDao {@ Autowired / / use the JDBC template private JdbcTemplate jdbcTemplate;} in the configuration file
Common methods of Spring Jdbc Template
We got the JDBC template above, and I'll show you how to use it. First of all, we need to understand the common methods of JdbcTemplate, and the common methods of this class are update and query.
1. Public int update (String sql,Object args [])
This method can add, modify and delete the data table. Use args [] to set the parameters in the SQL statement and return the number of updated rows.
Examples are as follows:
Public void add () {String insertSql = "insert into user values (null,?,?)"; Object parem1 [] = {"success", "123456"}; jdbcTemplate.update (insertSql,parem1); System.out.println ("the addition function in UserDao is implemented");}
2. Public List query (String sql,RowMapper rowMapper,Object args [])
This method can query the data table, and rowMapper maps the result set to a user-defined class (provided that the attributes in the custom class correspond to the fields of the data table).
Examples are as follows:
Public void query () {String selectSql = "select * from user"; RowMapper rowMapper = new BeanPropertyRowMapper (User.class); List list = jdbcTemplate.query (selectSql,rowMapper,null); System.out.println ("query function in UserDao is implemented");}
Case assistance
Pom.xml
Junit junit 4.11 test javax.servlet javax.servlet-api 3.1.0 javax.servlet jstl 1.2 mysql mysql-connector-java 5.1.38 org.springframework spring-core 5.1.5.RELEASE org.springframework spring-beans 5.1.5.RELEASE org.springframework spring-context 5.1.5.RELEASE org.springframework spring-aop 5.1.5.RELEASE org.springframework spring-jdbc 5.1.5.RELEASE org.springframework spring-web 5 .1.5.RELEASE org.springframework spring-webmvc 5.1.5.RELEASE org.springframework spring-expression 5.1.5.RELEASE org.springframework spring-tx 5.1.5.RELEASE commons-logging commons-logging 1.2
Spring-config.xml
User
Package com.my.pojo; public class User {private int id; private String username; private String password; public User () {} public User (int id, String username, String password) {this.id = id; this.username = username; this.password = password;} public int getId () {return id;} public void setId (int id) {this.id = id;} public String getUsername () {return username;} public void setUsername (String username) {this.username = username } public String getPassword () {return password;} public void setPassword (String password) {this.password = password;} @ Override public String toString () {return "User {" + "id=" + id + ", username='" + username +''+ ", password='" + password +''+'}';}}
UserDao
Package com.my.dao; public interface UserDao {public void add (); public void delete (); public void update (); public void query ();}
UserDaoImpl
@ Override public void update () {String updateSql = "update user set username=?, password=?" Where id =? "; Object parem3 [] = {" modify "," 654321 ", 3}; jdbcTemplate.update (updateSql,parem3); System.out.println (" modify function in UserDao ");} @ Override public void query () {String selectSql =" select * from user "; RowMapper rowMapper = new BeanPropertyRowMapper (User.class); List list = jdbcTemplate.query (selectSql,rowMapper,null); System.out.println (" query function in UserDao is implemented ") For (User user: list) {System.out.println (user);}
Test Test3
Package com.my.test; import com.my.dao.UserDao; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test3 {@ Autowired private UserDao userDao; public static void main (String [] args) {ApplicationContext appCon = new ClassPathXmlApplicationContext ("spring-config.xml"); / / get the target object UserDao userDao = (UserDao) appCon.getBean ("userDao") from the container; / / userDao.add () UserDao.delete (); userDao.update (); userDao.query ();}}
Test result
The above is how Spring carries on the database programming, the editor believes that there are some knowledge points that we may see or use in our daily work. I hope you can learn more from this article. For more details, please follow the industry information channel.
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.