The following cases are from the "Spring 3.X Enterprise Application Development Practice" this book.
For my week's groping, now summed up a few easy wrong point, of course, this is in my own made mistakes in the premise of summing up, if there is said not in place, welcome to point out. The code used by the "Spring 3.X Enterprise Application Development Combat" Chapter2, the code is not important, the difference is not small, mainly configuration prone to problems.
1.UserDao of code
Package Com.baobaotao.dao;
Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.jdbc.core.JdbcTemplate;ImportOrg.springframework.jdbc.core.RowCallbackHandler;Importorg.springframework.stereotype.Repository;ImportCom.baobaotao.domain.User;ImportJava.sql.ResultSet;Importjava.sql.SQLException;/*** Created by RACHEL on 2017/10/11.*/@Repository Public classUserdao {@Autowired//automatic injection of jdbctemplate beans PrivateJdbcTemplate JdbcTemplate; PublicUser Finduserbyusername (FinalString UserName) { //querying the user's SQL statements based on the user nameString sqlstr = "Select User_id,user_name,credits from T_user where user_name=?"; FinalUser User =NewUser (); Jdbctemplate.query (Sqlstr,NewObject[]{username},NewRowCallbackHandler () { Public voidProcessrow (ResultSet ResultSet)throwsSQLException {user.setuserid (Resultset.getint ("USER_ID")); User.setusername (UserName); User.setcredits (Resultset.getint ("Credits")); } }); returnuser; } Public intGetmatchcount (String userName, String password) {string Sqlstr= "SELECT count (*) from T_user where user_name=? and password=? "; Object args[]=Newobject[]{username, password}; return jdbctemplate.queryforobject (sqlstr, new object[] {userName, password},Int.class ); } Public voidupdatelogininfo (user user) {String sqlstr= "Update t_user set last_visit=?,last_ip=?,credits=?" where user_id=? "; Object args[]=Newobject[]{user.getlastvisit (), User.getlastip (), User.getcredits (), User.getuserid ()}; Jdbctemplate.update (sqlstr, args); }}
Note that the code that is marked is slightly different from the book. The book is queryForInt (...). ) method, and I replaced the queryForObject method because: After Spring 3.2.2, the queryForInt in JdbcTemplate has been canceled, and now, All with queryForObject (including queryForLong),queryForObject can replace the queryForInt method. about this question, recommend a blog: http://jackyrong.iteye.com/blog/2086255
Now that we've talked about method queryForObject, here are a few things to note about this approach:
Public int Getmatchcount (String userName, string password) { = "SELECT count (*) from T_user where user_name=? and password=? " ; New object[]{username, password}; return New Object[] {userName, password},integer. class ); Note that this code is different from the book, if you use the code in the book will definitely error }
queryForObject method :queryforobject (String sql, object[] args, class<t> requiredtype) {}
(1) Jdbctemple.queryforobject returns a single row of data, which means that only one record can be returned
(2) queryForObject () method, the third parameter is used to write the type of the return value, if you need to return an int type, write integer.class, you need to return a long type to write Long.class.
2.UserServiceTest
Package Com.baobaotao. Service;Importorg.junit.Test;ImportOrg.junit.runner.RunWith;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.test.context.ContextConfiguration;ImportOrg.springframework.test.context.junit4.SpringJUnit4ClassRunner;ImportCom.baobaotao.domain.User;ImportCom.baobaotao.services.UserService;Import Staticorg.junit.Assert.assertTrue;/*** Created by RACHEL on 2017/10/11.*/@RunWith (Springjunit4classrunner.class) @ContextConfiguration (Locations={"/applicationcontext.xml"})//Assign Spring profile Public classuserservicetest {@AutowiredPrivateUserService UserService; @Test//mark the manner of Test Public voidHasmatchuser ()throwsException {BooleanB1 = Userservice.hasmatchuser ("AA", "123"); //Note that this test code should not be written, it should be changed to your user name and password in the database;
BooleanB2 = Userservice.hasmatchuser ("AA", "1223"); In other words, the user name and password set here must be the same as what you set in the database, otherwise you will not find the page asserttrue (B1); Asserttrue (!B2); } @Test Public voidFinduserbyusername ()throwsException {User User= Userservice.finduserbyusername ("AA"); //The user name must match the user name set by the database }}
3.web.xml file
<servlet> <servlet-name>demo7</servlet-name> <servlet-class> Org.springframework.web.servlet.DispatcherServlet </servlet-class> < Load-on-startup>3</load-on-startup></servlet><servlet-mapping> <servlet-name> Demo7</servlet-name> <url-pattern>*.html</url-pattern></servlet-mapping></ Web-app>
Attention,<servlet-name>......</servlet-name>, must be your project name, do not write wrong into Baobaotao. I was for this reason, has been error, 404 can not find the page, Daoteng a day. But if you set the project name and the file name below main to Baobaotao, there will be no more.
4.servlet.xml file
<Bean class= "Org.springframework.web.servlet.view.InternalResourceViewResolver" p:viewclass= "Org.springframework.web.servlet.view.JstlView" p:prefix= "/web-inf/" p:suffix= ". jsp"/></beans>
If you do not build the JSP folder under Web-inf, but directly put main.jsp and login.jsp directly under the Web-inf, p:prefix= "/web-inf/jsp". This code represents the storage path of your. jsp file, so you must have a path that corresponds to your own, otherwise you will not find the path to report 404 error.
Finally, to recommend a good blog post, when learning, my help is still quite large, to encourage: http://www.cnblogs.com/yangyquin/p/5289604.html
Using the spring framework to implement user login instances