Software 152 class Yang JinhongSpring uses JdbcTemplate, Jdbcdaosupport, and namedparameterjdbctemplate to manipulate databases, but JdbcTemplate is most commonly used and easiest to use.Jdbc.properties:user=rootpassword=123driverclass=com.mysql.jdbc.driverjdbcurl=jdbc\:mysql\:///spring? Encoding\=uft-8initpoolsize=5maxpoolsize=20 Applicationcontext.xml Impor
In all previous spring boot and spring cloud related posts, the creation of the spring boot project will be involved. There are many ways to create them, either manually by Maven or through scaffolding, or by using the page tools mentioned in the article Springboot QuickStar
Today I'm going to show you about JdbcTemplate implementing queries and additions to the databaseThe first is to addFirst step, everybody knows. Create an entity classThen write a method to pass the entity class as a parameter.In implementing this interfaceJdbcdaosupport This must be a jar package for spring to inherit it to interact with the databaseThe service layer and DAO write a similar interface to th
The Project introduces a helper class (JDBC Template) for JDBC in spring that encapsulates the operation of JDBC and uses it to build a project project structure to create a MAVEN project, as shown in the figure above Maven dependency jar Rollup
New test class, creating data table
Package com.curd.spring.test;
Import Org.junit.Test;
Import org.springframework.jdbc.core.JdbcTemplate;
Import Org.springframework.jdbc.datasource.DriverManagerDa
directory, remember to add Common-logging.jar Also, spring log is used by default. Put the database configuration file into the Config directory. The contents of the Jdbc.properties file are as follows: Java code Jdbc.driverclassname=com.mysql.jdbc.driver Jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/report Jdbc.username=root jdbc.password=12345 Here's the code for configuring the data source: XML code In Configuring a
Application:Use spring's jdbctemplate to query the database, get a list of lists of results, database table fields and entity classes automatically correspond, you can use beanpropertyrowmapper.Note: The Beanpropertyrowmapper implements the RowMapper interface.Attention:Automatic binding requires the same column name as the Java entity class name, such as: The property name "UserName" can match the column field "UserName" or "user_name" in the databas
Here's a step-by-step procedure for implementing a custom paging using the spring JdbcTemplate method:
Step one to create the Splitpageresultsetextractor class:
Package com.utils;
Import Java.sql.ResultSet;Import java.sql.SQLException;Import java.util.ArrayList;Import java.util.List;
Import org.springframework.dao.DataAccessException;Import Org.springframework.jdbc.core.ResultSetExtractor;Import Org.springf
JdbcTemplate found a problem when it was used:Call the queryForObject () method and throw an exception if nothing is found: org.springframework.dao.EmptyResultDataAccessException:Incorrect result size:expected 1, actual 0You do not want to throw this exception, but return to null on the line.
See Source code
@Override public
The result set in the source code returns a static method that calls Dataaccessutils, and
1. Querying a row of data and returning an int type resultjdbcTemplate.queryForInt("select count(*) from test"); 2. Query a row of data and convert the row data to a map returnjdbcTemplate.queryForMap("select * from test where name=‘name5‘"); 3. Query a row for any type of data, and the last parameter specifies the type of return resultjdbcTemplate.queryForObject("select count(*) from test", Integer.class); 4. Query a batch of data, by default convert each row of data to mapjdbcTemplate.query
1. Querying object Public Classify Queryclassifbyid (int ID) { String sql= "SELECT * from T_classify where id=?" ; New object[] {ID}; = Jdbctemplate.queryforobject (sql, args, parameterizedbeanpropertyrowmapper.newinstance (classify. Class)); return classify; }2. Query List Public List Getallmenu () { String sql= "SELECT * from t_classify"; List Class)); return list; }3. Number of query records Public int GetCount () {
1. Bulk Update InsertsJdbctemplate.batchupdate (String sql, listObject[] The length of the array is the number of arguments per record (i.e. "?" in the SQL statement). Number of)List: How many records are inserted in total./** * BULK INSERT Order Details * */public void Addbatch (list2. Single record insert get primary key ID/** * insert and get primary key */public int Addandgetid (order order) { final String sql = "INSERT into orders (name, Address,createt Ime,totalprice,status) VALUES (?
Tags: contex xmla user Autowire pre classpath ring turn col1, first configure the JdbcTemplate Configure a JdbcTemplate bean in the Spring IOC container, inject DataSource (using a third-party data source c3p0), and inject jdbctemplate into the custom DAO. "DS"class="Com.mchange.v2.c3p0.ComboPooledDataSource"> "User"Va
Using spring's own jdbctemplate, you can simplify the operation of the database, it is very convenient to use. With the configuration of a few steps, you can use JdbcTemplate.(1) Configure the spring data source, add the MySQL driver jar package, configure the database properties file.To join the MySQL driver jar package, the steps to configure the database's pro
Another spring feature module data access supports the databaseSpring data Access First Helloword case:Implementing access configuration using Java programs1. Guide Package2. Test case@Testpublic void test01 () {Drivermanagerdatasource datasource=new drivermanagerdatasource (); Datasource.setdriverclassname ("Com.mysql.jdbc.Driver");d atasource.seturl ("Jdbc:mysql:///springtest"); Datasource.setusername ("root");d Atasource.setpassword ("123");
Tags: spring-database operations via JdbcTemplate-code implementation no configuration fileFirst, create a spring projectProject Name: spring101301Second, add the relevant jar in the spring project1. Create a Lib directory on the project/lib2. Add spring-supported jar packag
We know that the Spring boot Autoconfiguration feature can determine which of the spring configurations should be used depending on the circumstances, not which one should be used, for example:
Is spring's jdbctemplate in classpath? If it is, and DataSource is present, a J
) VALUES (?,?)"; intCount=jdbctemplate.update (SQL, "Doomsday", "20171125"); if(count==1) {
return true; }
return false; }
} Adding a database operation using the JdbcTemplate object to use the Update method directly from the data source is much easier than before. Before you finish, you need to press the following method: PreparedStatement ps=conn.preparestatement ("INSERT into Sw_user (Username,password) VALUES (?,?)"); Ps.setstr
Label: This blog uses Spring jdbctemplate to implement dynamic table-building. Described earlier, it encapsulates the basic operation of the database, let us use more flexible, the following to combat. 1. Preparatory work introduced Jar Package 2,applicationcontext.xml 3. Code private static ApplicationContext context = NULL; Test the public static void main (string[] args) {context = new C
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.