Action(Controller)====
package com.action;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.service.UserService;
@Controller
public class UserAction {
@Resource(name = "userService")
private UserService userService;
public void setUserService(UserService userService) {
this.userService = userService;
}
@RequestMapping(value = "/login")
public String load() {
System.out.println("=============1");
userService.find_all();
return "/index";
}
}
================
Service===
package com.service.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import com.dao.user.UserDao;
import com.entity.User;
import com.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
@Resource(name="userDao")
private UserDao userDao;
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
@Override
public List<User> find_all() {
return userDao.find_all();
}
@Override
public List<User> find_fuzzy(String key) {
// TODO Auto-generated method stub
return null;
}
@Override
public void add(User t) {
// TODO Auto-generated method stub
}
@Override
public void delete(User t) {
// TODO Auto-generated method stub
}
@Override
public User update(User t) {
// TODO Auto-generated method stub
return null;
}
@Override
public User findBy_Id(User t) {
// TODO Auto-generated method stub
return null;
}
//org.s...f....web.servlet.view.InternalResourceViewResolver
}
Dao ===
package com.dao.user.impl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import com.dao.user.UserDao;
import com.entity.User;
@Repository("userDao")
public class UserDaoImpl implements UserDao {
@Resource(name="jdbcTemplate")
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<User> find_all() {
String sql = "select name from user where id=?";
/*String name=(String)jdbcTemplate.queryForObject(
"selcet name from user where id=?",
new Object[]{id},
java.lang.String.class);
)*/
System.out.println(jdbcTemplate.queryForList(sql,new Object[]{1},String.class));
return null;
}
@Override
public List<User> find_fuzzy(String key) {
// TODO Auto-generated method stub
return null;
}
@Override
public void add(User t) {
// TODO Auto-generated method stub
}
@Override
public void delete(User t) {
// TODO Auto-generated method stub
}
@Override
public User update(User t) {
// TODO Auto-generated method stub
return null;
}
@Override
public User findBy_Id(User t) {
// TODO Auto-generated method stub
return null;
}
}
applicationContext.xml====
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" <pre name="code" class="html">Web.xml
====
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>LoveFood_Spring</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param> -->
<!-- 后加 -->
<servlet>
<servlet-name>spring-mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>spring-mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd "> <import resource="db-mysql.xml"/> <!-- 数据Bean --><bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="dataSource_Mysql" /> </property></bean> <!--User Bean --><!-- <bean id="userDao" class="com.dao.user.impl.UserDaoImpl"> <property name="jdbcTemplate"> <ref bean="jdbcTemplate" /> </property> </bean> <bean id="userService" class="com.service.impl.UserServiceImpl"> <property name="userDao"> <ref bean="userDao" /> </property> </bean> <bean id="UserAction" class="com.action.UserAction"> <property name="userService"> <ref bean="userService" /> </property> </bean> --> <!-- --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/View"/> <property name="suffix" value=".jsp"/> </bean> <mvc:annotation-driven/> <context:component-scan base-package="com"/> <!-- 注解驱动 --> </beans>
db-mysql.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd">
<bean id="dataSource_Mysql" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/lovefood" />
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="idleConnectionTestPeriod" value="60"/>
<property name="idleMaxAge" value="240"/>
<property name="maxConnectionsPerPartition" value="30"/>
<property name="minConnectionsPerPartition" value="10"/>
<property name="partitionCount" value="3"/>
<property name="acquireIncrement" value="5"/>
<property name="statementsCacheSize" value="100"/>
<property name="releaseHelperThreads" value="3"/>
</bean>
</beans>