Spring provides a lot of template classes for DAO support, such as Hibernatetemplate, JdbcTemplate, and so on, for example. Take a look at a demo
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package Com.test;import Org.springframework.jdbc.core.jdbctemplate;import Org.springframework.jdbc.datasource.drivermanagerdatasource;public class Jdbctest {public static void main (string[] args) {//Create an DataSource object that encapsulates the database connection information Drivermanagerdatasource datasource=new drivermanagerdatasource ();// The Information datasource.setdriverclassname ("Com.mysql.jdbc.Driver") for which to specify the connection database is;d atasource.seturl ("Jdbc:mysql://localhost : 3306/test ");d atasource.setusername (" root ");d Atasource.setpassword (" ");//Using a template class, you must first have an object of the template class, It must be provided with database-related connection information jdbctemplate template=new jdbctemplate (DataSource);//Run Operation Template.execute ("INSERT INTO News_title VALUES (' 54 ', ' 54 ', ' 354 ', ' 6345 ');}} </span>
The above simply uses a jdbctemplate. In spring, we are provided with very many template classes for DAO, examples JdbcTemplate, hibernatetemplate, Sqlmapclienttemplate (OBSOLETE), jpatemplate (obsolete), etc. Take Jdbcdaosupport, for example, to look at the configuration of the detailed data source.
Idao Configuration
Idao is the underlying interface class that provides data access capabilities
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package Cn.itast.tx.account;public interface Accountdao {public void Inmoney (String in,double money);p ublic void Outmoney (String out,double money);} </span>
DAO implements the Idao, encapsulates the function of the detailed data interview
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" >package cn.itast.template.jdbc;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 Cn.itast.template.jdbc.vo.usermodel;public class Userdaoimpl extends jdbcdaosupport{/*//inherits the DAO support abstract class. The method of injecting itself into it is the ability to inject a template object into it and inject it into the data source private jdbctemplate jdbctemplate;public void Setjdbctemplate (JdbcTemplate JdbcTemplate) {this.jdbctemplate = JdbcTemplate;} */public void Add (Usermodel um) {//string sql = "INSERT into tbl_user values (null, '" +um.getusername () + "'," +um.getage () + " );//Method This.getjdbctemplate ()//this.getjdbctemplate (). Execute (SQL) for the template object. String sql = "INSERT into tbl_user values (null,?,?)"; This.getjdbctemplate (). Update (SQL, Um.getusername (), Um.getage ());} public void Delete (Usermodel um) {String-sql = "Delete from tbl_user where uuid =" +um.getuuid (); This.getjdbctemplate (). exe cute (SQL);} public void UPDATe (Usermodel um) {//string sql = "Update tbl_user set userName = '" +um.getusername () + "', age =" +um.getage () + "where uuid = "+um.getuuid ();//this.getjdbctemplate (). Execute (SQL); String sql = "Update tbl_user set username=?", age=?
where uuid =?
"; This.getjdbctemplate (). Update (SQL, Um.getusername (), Um.getage (), Um.getuuid ());} Public String Getnamebyuuid (Long uuid) {String sql = ' Select UserName from tbl_user where uuid =? '; Return This.getjdbctemplate (). queryForObject (SQL, String.class, uuid);} Public Long GetCount () {String-sql = "SELECT count (UUID) from Tbl_user"; return This.getjdbctemplate (). queryForLong (SQL);} Public Usermodel get (Long uuid) {String sql = "SELECT * from tbl_user where uuid =?"; rowmapper<usermodel> rm = new Rowmapper<usermodel> () {public Usermodel Maprow (ResultSet rs, int rowNum) Throws SQLException {Usermodel um = new Usermodel (); Um.setuuid (Rs.getlong ("uuid")); Um.setusername (Rs.getstring (" UserName ")); Um.setage (Rs.getint (" Age ")); return um;}}; Return This.getjdbctemplate (). queryForObject (Sql,rm,uuid);} Public list<usermodel> GetAll () {String-sql = "SELECT * from Tbl_user"; rowmapper<usermodel> rm = new Rowmapper<usermodel> () {public Usermodel Maprow (ResultSet rs, int rowNum) Throws Sqlexception {Usermodel um = new Usermodel (); Um.setuuid (Rs.getlong ("uuid")); Um.setusername (rs.getstring ("UserName")); Um.setage (Rs.getint ("Age")); return um;}}; Return this.getjdbctemplate (). query (sql, RM);}} </span>
injection of detailed data sources
Because Jdbcdaosupport needs datasource injection.
<span style= "FONT-FAMILY:SIMSUN;FONT-SIZE:18PX;" ><?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:context= "http://www.springframework.org/schema/ Context "xsi:schemalocation=" Http://www.springframework.org/schema/beans http://www.springframework.org/schema/ Beans/spring-beans.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/ Context/spring-context.xsd "><!--DAO--><bean id=" Userdao "class=" Cn.itast.template.jdbc.UserDaoImpl " ><!--<property name= "JdbcTemplate" ref= "JdbcTemplate"/>--><property name= "DataSource" ref= " DataSource "/></bean><!--jdbctemplate--><!--<bean id=" JdbcTemplate "class=" Org.springframework.jdbc.core.JdbcTemplate "><property name=" DataSource "ref=" DataSource "/></bean> --><!--DataSource--><bean id= "DataSource" class="Org.springframework.jdbc.datasource.DriverManagerDataSource" ><property name= "Driverclassname" value= " Com.mysql.jdbc.Driver "/><property name=" url "value=" Jdbc:mysql://localhost:3306/springdb "/><property Name= "username" value= "root"/><property name= "password" value= "root"/></bean></beans></ Span>
Analysis: The above is just an injection of a simple data source, and spring provides us with very much. However, all of the configuration methods are consistent.