Web. xml: (Web. xml)
<!--loading the spring container - <Context-param> <Param-name>Contextconfiglocation</Param-name> <Param-value>Classpath:spring.xml,classpath:spring-mybatis.xml,classpath:spring-shiro.xml</Param-value> </Context-param>
Mybatis.xml: (Spring-mybatis.xml)
<?XML version= "1.0" encoding= "UTF-8"?><Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"XMLNS:AOP= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP"Xmlns:context= "Http://www.springframework.org/schema/context"Xmlns:mvc= "Http://www.springframework.org/schema/mvc"Xmlns:tx= "Http://www.springframework.org/schema/tx"xsi:schemalocation= "Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/ Spring-context-4.3.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP http://www.springframework.org/schema/aop/ Spring-aop-4.3.xsd Http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/ Spring-tx-4.3.xsd "> <!--Import Resource File - <!--load the contents of the Db.properties file, the key name in the Db.properties file must have some special rules - <Context:property-placeholder Location= "Classpath:jdbc.properties" /> <!--configuring C3P0 Data Sources - <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource"> <!--connection parameters for the database - < Propertyname= "User"value= "${jdbc.user}"></ Property> < Propertyname= "Password"value= "${jdbc.password}"></ Property> < Propertyname= "Driverclass"value= "${jdbc.driverclass}"></ Property> < Propertyname= "Jdbcurl"value= "${jdbc.jdbcurl}"></ Property> < Propertyname= "Initialpoolsize"value= "${jdbc.initalpoolsize}"></ Property> < Propertyname= "Maxpoolsize"value= "${jdbc.maxpoolsize}"></ Property> </Bean> <!--Configure Namedparameterjdbctemplate, which can make the appliance name parameter, which has no parameterless constructor, must specify parameters for its constructor - <BeanID= "Namedparameterjdbctemplate"class= "Org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <Constructor-argref= "DataSource"></Constructor-arg> </Bean></Beans>
Userdao.java
@Repository Public classUserdao { PublicUser finduserbyloginname (String loginName) {String sql= "SELECT * from user where LoginName =: LoginName"; User User=NewUser (); User.setloginname (LoginName); //User.setpassword (PassWord);Sqlparametersource Parametersource =Newbeanpropertysqlparametersource (user); Beanpropertyrowmapper<User> RowMapper =NewBeanpropertyrowmapper<user> (User.class); NewJdbcmysql (); Namedparameterjdbctemplate namedparameterjdbctemplate=jdbcmysql.namedparameterjdbctemplate; Try{User=Namedparameterjdbctemplate.queryforobject (Sql,parametersource,rowmapper); } Catch(DataAccessException e) {return NULL; } returnuser; }}
Jdbcmysql.java
Public classJdbcmysql {Private StaticApplicationContext CTX =NULL; Public Staticnamedparameterjdbctemplate namedparameterjdbctemplate; { Try { if(CTX = =NULL) {CTX=NewClasspathxmlapplicationcontext ("Spring-mybatis.xml"); } namedparameterjdbctemplate= (namedparameterjdbctemplate) Ctx.getbean (namedparameterjdbctemplate.class); /*combopooleddatasource pool= (combopooleddatasource) Ctx.getbean ("DataSource"); Jdbcuser = Pool.getuser (); Jdbcpassword = Pool.getpassword (); Jdbcurl = Pool.getjdbcurl (); Driverclass = Pool.getdriverclass (); Initialpoolsize = Pool.getinitialpoolsize (); Maxpoolsize = Pool.getmaxpoolsize ();*/ } Catch(Exception e) {System.out.println ("Error:" +e.getmessage () +e.getstacktrace ()); } }}
JAVA-JDBC Database Connection