The previous article was just a simple spring MVC framework and then added some interaction with the database.
First, add JDBC-related configurationAfter adding dependent dependencies in Maven, configure database access parameters and data sources. Database parameters using the configuration file, the code is as follows: Jdbc.properties
1Jdbc.driver=Com.mysql.jdbc.Driver2Jdbc.url=Jdbc:mysql://localhost3306/SampleDb3Jdbc.username=Root4Jdbc.password=1234565Jdbc.initialsize=56Jdbc.maxactive=Ten7Jdbc.minidle=58Jdbc.maxidle=Ten9Jdbc.timebetweenevictionrunsmillis=3600000TenJdbc.minevictableidletimemillis=3600000 OneJdbc.testonborrow=true AJdbc.validationquery=SELECT 1 fromDUAL
Add data source configuration and JdbcTemplate configuration in App-servlet.xml:
<BeanID= "Jdbcconfig"class= "Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> < Propertyname= "Location"> <value>Classpath:jdbc.properties</value> </ Property></Bean><BeanID= "Databasesource"class= "Org.apache.commons.dbcp.BasicDataSource"Destroy-method= "Close"> < Propertyname= "Driverclassname"value= "${jdbc.driver}"/> < Propertyname= "url"value= "${jdbc.url}"/> < Propertyname= "username"value= "${jdbc.username}"/> < Propertyname= "Password"value= "${jdbc.password}"/> < Propertyname= "Maxactive"value= "${jdbc.maxactive}"/> < Propertyname= "InitialSize"value= "${jdbc.initialsize}"/> < Propertyname= "Maxidle"value= "${jdbc.maxidle}"/> < Propertyname= "Minidle"value= "${jdbc.minidle}"/> < Propertyname= "Timebetweenevictionrunsmillis"value= "${jdbc.timebetweenevictionrunsmillis}"/> < Propertyname= "Minevictableidletimemillis"value= "${jdbc.minevictableidletimemillis}"/> < Propertyname= "Testonborrow"value= "${jdbc.testonborrow}"/> < Propertyname= "Validationquery"value= "${jdbc.validationquery}"/></Bean><BeanID= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate"> < Propertyname= "DataSource"ref= "Databasesource"/></Bean>
Second, the implementation of a user named Super, password 123456 users login process
2.1 Adding a user entity classThe User.java code is as follows:
1 Public classUser {2 PrivateLong ID;3 PrivateString username;4 PrivateString Email;5 PrivateString password;6 PrivateLong Credit;7 PrivateDate lastvisittime;8 PrivateString Lastvisitip;9 //Setter, GetterTen}
2.2 Implementing access to a databaseNew Userdao.java, adding @repository annotations to the Userdao class first assemble jdbctemplate in the class, using @autowired annotations
@Autowired Private JdbcTemplate JdbcTemplate;
Then add a method that queries the user based on the user name and password:
1 PublicUser Finduser (FinalString username,FinalString Password) {2String sql = "Select Id,user_name, credits from t_user_info where user_name =?" and password =? ";3User User =NULL;4 Try {5rowmapper<user> rm = Parameterizedbeanpropertyrowmapper.newinstance (User.class);6user = (user) jdbctemplate.queryforobject (SQL,Newobject[]{username, password}, RM);7}Catch(Exception e) {8 e.printstacktrace ();9 }Ten returnuser; One}
2.3 Add the Userservice.java class with the following code:
1@Service ("UserService")2 Public classUserService {3 @Autowired4 Userdao Userdao;5 PublicUser finduser (String username,string password) {6 returnUserdao.finduser (Username,password);7 }8 9 Public intfindusercount (String username,string password) {Ten returnUserdao.findusercount (Username,password); One } A - Public Booleaninsertuser (user user) { - returnuserdao.insertuser (user); the } -}
2.4 Modifying the Userindex method of Usercontroller.java:
1@RequestMapping (value = "/index.html", method =requestmethod.post)2 Publicmodelandview Userindex (string Username, string password) {3 if(Stringutil.isempty (username) | |stringutil.isempty (password)) {4Logger.error ("User name or password is empty");5Modelandview Modelandview =NewModelandview ("/index");6Modelandview.addobject ("Error", "User name or password is empty!") ");7 returnModelandview;8 }9 Ten //int count = Userservice.findusercount (Username,password); OneUser User =userservice.finduser (username, password); A if(User = =NULL) { -Logger.info ("User name or password error"); -Modelandview Modelandview =NewModelandview ("/index"); theModelandview.addobject ("username", username); -Modelandview.addobject ("Error", "User:" + username + "Not present or user password error!) "); - returnModelandview; - } +Modelandview Mav =NewModelandview ("Success"); -Mav.addobject ("username", username); +Mav.addobject ("Password", password); ALogger.info ("Username:" + username + ", Password:" +password); at returnMav; -}
This enables a simple process of login.
Spring MVC chapter II, using JdbcTemplate for database operations