`

spring mvc + spring + spring jdbc

阅读更多
spring3.2.4,spring mvc,spring jdbc
采用spring
项目的工程图:

导入jar包:


1.web.xml 设置:
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
	xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
	<display-name>bkoef</display-name>
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>
			classpath:applicationContext.xml
			classpath:com/test/test/spring-context-test.xml
		</param-value>
	</context-param>

	<filter>
		<filter-name>characterEncodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>

	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<servlet>
		<servlet-name>spring-mvc</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<load-on-startup>4</load-on-startup>
	</servlet>

	<servlet-mapping>
		<servlet-name>spring-mvc</servlet-name>
		<url-pattern>/controller/*</url-pattern>
	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.html</welcome-file>
	</welcome-file-list>
</web-app>


2.applicationContext.xml
<?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:security="http://www.springframework.org/schema/security"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:util="http://www.springframework.org/schema/util"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <context:annotation-config/>
	<aop:aspectj-autoproxy/>
	<tx:annotation-driven/>
 	
	<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name = "dataSource"  ref="dataSource"/>
	</bean>
	
	<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="locations" ref="propertyLocations"/>
	</bean>
	
	<util:list id="propertyLocations">
		<value>classpath:jdbc.properties</value>
	</util:list>
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
		<property name="url"><value>${jdbc.url}</value></property>
		<property name="username"><value>${jdbc.username}</value></property>
		<property name="password"><value>${jdbc.password}</value></property>
		
        <property name="validationQuery"><value>${jdbc.validationQuery}</value></property>
        <property name="testOnBorrow"><value>${jdbc.testOnBorrow}</value></property>
       
        <property name="maxActive"><value>${jdbc.maxActive}</value></property>
        <property name="maxIdle"><value>${jdbc.maxIdle}</value></property>
        <property name="maxWait"><value>${jdbc.maxWait}</value></property>
	</bean>
	
</beans>


3.spring-mvc-servlet.xml
<?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:p="http://www.springframework.org/schema/p"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
    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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd">
		
	<context:annotation-config/>
	<mvc:annotation-driven />
	
	<context:component-scan base-package="com.test.test.web.controller"/>
	
</beans>

4.spring-context-test.xml
<?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"
	xmlns:util="http://www.springframework.org/schema/util"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
	
	<bean id="stringGenerator" class="org.overdijk.commons.string.generator.BasicStringGenerator"/>
		
	<bean id="testDao" class="com.test.test.dao.impl.TestDaoImpl">
		<property name="dataSource" ref="dataSource" />
	</bean>
	
	<bean id="testService" class="com.test.test.service.impl.TestServiceImpl">
	</bean>
</beans>

5.vo
public class User {
  private String username;
  private String password;
  /**
   getter();
   setter();
  **/
}

6.dao
public interface TestDao {
	public List<User> getUserList();
}

7.daoimpl
public class TestDaoImpl extends NamedParameterJdbcDaoSupport implements TestDao{

private final static RowMapper<User> USER_MAPPER = BeanPropertyRowMapper.newInstance(User.class);
	
	private final static String SQL_FINDUSER ="SELECT * from sys_user";
	
	@Override
	public List<User> getUserList() {
		return this.getNamedParameterJdbcTemplate().query(SQL_FINDUSER, USER_MAPPER);
	}

}

8.service
public interface TestService {
	public List<User> getUsers();
}

9.serviceImpl
public class TestServiceImpl implements TestService {

	@Autowired
	private TestDao testDao;
	
	@Override
	public List<User> getUsers() {
		return testDao.getUserList();
	}
}

10.Controller
@Controller
@RequestMapping("/test")
public class TestController {
	
	private static Log log = LogFactory.getLog(TestController.class);

	@Autowired
	private TestServiceImpl testService;
	
	@RequestMapping(value="getUsers",method = RequestMethod.GET)
	public @ResponseBody List<User> getUsers(){
		log.info("[TestController.getUsers]");
		List<User> users = this.testService.getUsers();
		for(User user: users){
			System.out.println(user.getUsername());
		}
		return this.testService.getUsers();
	}
}

11.测试:localhost:8080/ProjectTest/controller/test/getUsers
12.结果:
[{"username":"123123","password":"123123"},{"username":"112233","password":"55"},{"username":"waaa","password":"aa"},{"username":"bb","password":"bb"}]

  • 大小: 11.8 KB
  • 大小: 21.5 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics