Tag: NET JDBC Client part strip-name CEP database connection location
Public Number: Compassblog
Welcome to pay attention to, forward, learn from each other and progress together!
Have any questions, please contact the background message!
1. Spring Framework integrates DAO template
- JDBC:org.springframework.jdbc.core.JdbcTemplate
- Hibernate3.0:org.springframework.orm.hibernate3.hibernatetemplate
- MyBatis:org.springframework.orm.ibatis.SqlMapClientTemplate
- JPA:org.springframework.orm.jpa.JpaTemplate
2. JdbcTemplate Template Integration JDBC instance
(1), create a new Web project, import the required jar package as shown in:
(2), in MySQL database jdbctemplatedb new data table T_user, as shown in:
(3), create a new bean class, specific code is as follows:
User.java
Package Com.spring.bean;PublicClassUser {Private Integer ID;private String name;Public IntegerGetId() {return ID; }Publicvoidsetid (Integer id) {this.id = ID;} Span class= "hljs-function" >public String getname () {return name;} public void setname (String name) {this.name = Name } @Override public String tostring () {return "User [id=" + ID + "name=" + name + "]"; }}
(4), writing DAO interface, the code is as follows:
UserDao.java
Package Com.spring.dao;Import java.util.List;Import Com.spring.bean.User;PublicInterfaceUserdao {Increasevoidsave (User u); //delete void delete (Integer ID); //change void update (User u); //check user getById (Integer ID); //check int gettotalcount (); //check list<user> getAll ();
(5), write a class to implement the DAO interface, the code is as follows:
UserDaoImpl.java
Package Com.spring.dao;Import Java.sql.ResultSet;Import java.sql.SQLException;Import java.util.List;Import Org.springframework.jdbc.core.RowMapper;Import Org.springframework.jdbc.core.support.JdbcDaoSupport;Import Com.spring.bean.User;Using the JDBC template to implement additions and deletionsPublicClassUserdaoimplExtendsJdbcdaosupportImplementsUserdao {PublicvoidSave(User u) {String sql ="INSERT into t_user values (null,?)";Super.getjdbctemplate (). Update (SQL, U.getname ()); }PublicvoidDelete(Integer ID) {String sql ="Delete from T_user where id =?";Super.getjdbctemplate (). Update (SQL,ID); }PublicvoidUpdate(User u) {String sql ="Update t_user set name =?" where id=? ";Super.getjdbctemplate (). Update (SQL, U.getname (), U.getid ()); }Public UserGetById(Integer ID) {String sql ="SELECT * from t_user where id =?";ReturnSuper.getjdbctemplate (). queryForObject (SQL,New Rowmapper<user> () {Public UserMaprow(ResultSet RS,int arg1)Throws SQLException {User U =New User (); U.setid (Rs.getint ("id")); U.setname (Rs.getstring ("Name"));return u; }}, id); }PublicIntGettotalcount() {String sql ="SELECT COUNT (*) from T_user"; Integer count =Super.getjdbctemplate (). queryForObject (SQL, Integer.class); return count;} public list<user> getAll< Span class= "Hljs-params" > () {String sql = "select * from T_user"; list<user> list = super.getjdbctemplate (). query (SQL, New Rowmapper<user> () {public User maprow (ResultSet rs, int arg1) throws SQLException {User U = new User (); U.setid (Rs.getint ( "id")); U.setname (rs.getstring ( "name")); return U;}); return list;}
(6), the new database-driven Link resource file, the code is as follows:
db.properties
jdbc.jdbcUrl=jdbc:mysql:///JdbcTemplateDBjdbc.driverClass=com.mysql.jdbc.Driverjdbc.user=rootjdbc.password=root
(7), write the Spring configuration file, the code is as follows:
applicationContext.xml
<?xml version= "1.0" encoding= "UTF-8"?><BeansXmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"xmlns="Http://www.springframework.org/schema/beans"xmlns:context="Http://www.springframework.org/schema/context"xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd "><!--specify spring read db.properties configuration--<Context:property-placeholderlocation="Classpath:db.properties"/><!--1. Put the database connection pool into the spring container--<BeanName="DataSource"class="Com.mchange.v2.c3p0.ComboPooledDataSource" ><PropertyName="Jdbcurl"Value="${jdbc.jdbcurl}" ></Property><PropertyName="Driverclass"Value="${jdbc.driverclass}" ></Property><PropertyName="User"Value="${jdbc.user}" ></Property><PropertyName="Password"Value="${jdbc.password}" ></Property></Bean><!--2. Place jdbctemplate into spring container--<BeanName="JdbcTemplate"class="Org.springframework.jdbc.core.JdbcTemplate" ><PropertyName="DataSource"ref= "DataSource" ></property>< Span class= "Hljs-tag" ></bean><!--3. Put Userdao into spring container--><bean name=< Span class= "hljs-string" > "Userdao" class= " Com.spring.dao.UserDaoImpl "> <property name= "DataSource" ref=" DataSource "></property> </bean></ BEANS>
(8), the new test class for testing, the code is as follows:
TestDemo.java
Package com.spring.test;Import Org.junit.Test;Import Org.junit.runner.RunWith;Import org.springframework.beans.factory.annotation.Autowired;Import Org.springframework.jdbc.core.JdbcTemplate;Import org.springframework.test.context.ContextConfiguration;Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;Import Com.mchange.v2.c3p0.ComboPooledDataSource;Import Com.spring.bean.User;Import Com.spring.dao.UserDao;Demo JDBC Template@RunWith (Springjunit4classrunner.class)@ContextConfiguration ("Classpath:applicationContext.xml")PublicClassTestdemo {@AutowiredPrivate Userdao ud;@TestPublicvoidFun1()Throws exception{1. Prepare the connection pool Combopooleddatasource DataSource =New Combopooleddatasource (); Datasource.setdriverclass ("Com.mysql.jdbc.Driver"); Datasource.setjdbcurl ("Jdbc:mysql:///jdbctemplatedb"); Datasource.setuser ("Root"); Datasource.setpassword ("Root");2. Creating a JDBC Template object JdbcTemplate JT =New JdbcTemplate (); Jt.setdatasource (DataSource);3. Write the SQL statement and execute String sql ="INSERT into t_user values (null, ' Rose ')"; Jt.update (SQL); }@TestPublicvoidFun2()Throws exception{User U =New User (); U.setname ("Tom"); Ud.save (U); }@TestPublicvoidFun3()Throws exception{User U =New User (); U.setid (2); U.setname ("Jack"); Ud.update (U); }@TestPublicvoidFun4()Throws exception{Ud.delete (2); } @Test public void fun5 () throws exception{System.out.println (Ud.gettotalcount ());} @Test public void fun6 () throws exception{System.out.println (Ud.getbyid (1));} @Test public void fun7 () throws exception{System.out.println (Ud.getall ());}}
(9), configure dependencies and use plots:
Note: Part of the knowledge from the network, infringement of the joint delete!
Scan attention to the public number, learn more
Example of JDBC consolidation for Spring Framework series