Spring Learning-day12

Source: Internet
Author: User

1. Configuration Files

<?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.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.0.xsd "> <!--Import resource Files--<context:property-placeholder location=" Classpath:db.propertie S "/> <!--configuration c3p0 database--<bean id=" DataSource "class=" Com.mchange.v2.c3p0.ComboPooledDataSource " > <property name= "User" value= "${jdbc.user}" ></property> <property name= "password" value= "        ${jdbc.password} "></property> <property name=" Jdbcurl "value=" ${jdbc.jdbcurl} "></property> <property name= "Driverclass" value= "${JDBC.DRIVERCL"></property> <property name=" initialpoolsize "value=" ${jdbc.initpoolsize} "></property> <property name= "maxpoolsize" value= "${jdbc.maxpoolsize}" ></property> </bean> <!--configuration Sprin        G JdbcTemplate--<bean id= "JdbcTemplate" class= "Org.springframework.jdbc.core.JdbcTemplate" > <property name= "DataSource" ref= "DataSource" ></property> </bean></beans>

2. JavaBean

jdbc.user=rootjdbc.password=adminjdbc.driverClass=com.mysql.jdbc.Driverjdbc.jdbcUrl=jdbc:mysql:///spring4jdbc.initPoolSize=5jdbc.maxPoolSize=10
  Package Com.atguigu.spring.jdbc;public class Grade {private int id;    private int math;    private int 中文版;    private int Chinese;    public int getId () {return id;    } public void setId (int id) {this.id = ID;    } public int Getmath () {return math;    } public void Setmath (int math) {this.math = math;    } public int Getenglish () {return 中文版;    } public void Setenglish (int 中文版) {this.english = 中文版;    } public int Getchinese () {return Chinese;    } public void Setchinese (int chinese) {This.chinese = Chinese;  } @Override Public String toString () {return "Grade [id=" + ID + ", math=" + Math + ", english=" + 中文版 +    ", chinese=" + Chinese + "]"; }}
package com.atguigu.spring.jdbc;public class User {    private int id;    private String useName;    private String email;    private Grade gradeId;    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    public String getUseName() {        return useName;    }    public void setUseName(String useName) {        this.useName = useName;    }    public String getEmail() {        return email;    }    public void setEmail(String email) {        this.email = email;    }    public Grade getGradeId() {        return gradeId;    }    public void setGradeId(Grade gradeId) {        this.gradeId = gradeId;    }    @Override    public String toString() {        return "Test [id=" + id + ", useName=" + useName + ", email=" + email + ", gradeId=" + gradeId + "]";    }}

3. Test class

Package Com.atguigu.spring.jdbc;import Com.atguigu.spring.jdbc.user;import Java.sql.sqlexception;import Java.util.arraylist;import Java.util.list;import Javax.sql.datasource;import Org.junit.test;import Org.springframework.context.applicationcontext;import Org.springframework.context.support.classpathxmlapplicationcontext;import Org.springframework.jdbc.core.beanpropertyrowmapper;import Org.springframework.jdbc.core.jdbctemplate;import    Org.springframework.jdbc.core.rowmapper;public class Jdbctest {private ApplicationContext CTX = null;    private JdbcTemplate jdbctemplate = null;        {CTX = new Classpathxmlapplicationcontext ("Applicationcontext.xml");    JdbcTemplate = (jdbctemplate) ctx.getbean ("JdbcTemplate"); }/** * Gets the value of a single column, or makes a statistical query * */@Test public void TestQueryForObject2 () {String sql = "SELECT Co        UNT (ID) from test ";        Long Count = Jdbctemplate.queryforobject (sql, Long.class);     System.out.println (count); }    /**     * Find the collection of entity classes * called Jdbctemplate.query method */@Test public void Testqueryforlist () {String sql = "Select        ID, usename, email from test where ID >? ";        rowmapper<user> RowMapper = new beanpropertyrowmapper<> (user.class);        list<user> users = jdbctemplate.query (sql, RowMapper, 5);    System.out.println (users); }/** * Gets a record from the database and actually gets a corresponding object * call queryForObject (SQL, RowMapper, 1) method * 1. Where the RowMapper specifies how to map the rows of the result set, the commonly used implementation class is Beanpropertyrowmapper * 2. Use aliases for columns in SQL to complete mapping of column names and class property names */@Test public void Testqueryforobject () {String SQL = "SELECT ID, Usename,        Email from test where id =? ";        rowmapper<user> RowMapper = new beanpropertyrowmapper<> (user.class);        User test = jdbctemplate.queryforobject (sql, RowMapper, 1);    SYSTEM.OUT.PRINTLN (test); /** * Perform batch updates: Batch insert, UPDATE, delete * The last parameter is the list type of objetc[]: Because modifying a record requires an array of Object, multiple OBJ is required for multiple entries An array of ECT     */@Test public void Testbatchupdate () {String sql = ' INSERT into Test (usename, email) values (?,?) ";        list<object[]> Batchargs = new arraylist<> ();        Batchargs.add (New object[]{"AAA", "[email protected]"});        Batchargs.add (New object[]{"BBB", "[email protected]"});        Batchargs.add (New object[]{"CCC", "[email protected]"});    Jdbctemplate.batchupdate (SQL, Batchargs); }/** * Performs INSERT, UPDATE, DELETE */@Test public void Testupdate () {String sql = "Update Test SE T usename =?        WHERE id =? ";    Jdbctemplate.update (SQL, "Tom", 2); } @Test public void Testdatasource () throws SQLException {DataSource DataSource = Ctx.getbean (datasource.cl        );    System.out.println (Datasource.getconnection ()); }}

Spring Learning-day12

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.