Spring Learning Note VI (Configuration of the data source)

Source: Internet
Author: User

1. Preface

The previous blog analyzes the mechanism of two dynamic proxies that implement AOP in spring, and this blog post explains the configuration of the data sources in spring.


2.DAO Supported template Classes

Spring provides a lot of template classes for DAO support, such as Hibernatetemplate, JdbcTemplate, and so on, for example, see 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);//Perform Operation Template.execute ("INSERT INTO News_title VALUES (' 54 ', ' 54 ', ' 354 ', ' 6345 ');}} </span>


3. Data Source Configuration

The above simply takes advantage of a jdbctemplate, and in spring provides us with a lot of DAO's template classes, examples JdbcTemplate, hibernatetemplate, Sqlmapclienttemplate (OBSOLETE), Jpatemplate (OBSOLETE) and so on, take Jdbcdaosupport as an example to look at the configuration of the specific data source.

Idao Configuration

Idao is the underlying interface class that provides the ability to access data

<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 specific data access

<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 injected method is automatically provided for it//it can be injected into a template object or it can be injected into a 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 a specific data source

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 a simple source of data injection, Spring provides us with a lot, but all configuration methods are consistent.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Spring Learning Note VI (Configuration of the data source)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.