mvc框架现在有时间,把它整理成博客便于记忆和查看
aopalliance-1.0.jar
asm-3.3.1.jar
aspectjweaver-1.6.11.jar
cglib-2.2.2.jar
commons-dbcp-1.2.2.jar
commons-logging-1.1.1.jar
commons-pool-1.3.jar
javassist-3.17.1-GA.jar
jstl-1.2.jar
junit-4.9.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
mybatis-spring-1.2.2.jar
ojdbc14.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
spring-aop-4.1.0.RELEASE.jar
spring-aspects-4.1.0.RELEASE.jar
spring-beans-4.1.0.RELEASE.jar
spring-context-4.1.0.RELEASE.jar
spring-context-support-4.1.0.RELEASE.jar
spring-core-4.1.0.RELEASE.jar
spring-expression-4.1.0.RELEASE.jar
spring-instrument-4.1.0.RELEASE.jar
spring-instrument-tomcat-4.1.0.RELEASE.jar
spring-jdbc-4.1.0.RELEASE.jar
spring-jms-4.1.0.RELEASE.jar
spring-orm-4.1.0.RELEASE.jar
spring-oxm-4.1.0.RELEASE.jar
spring-test-4.1.0.RELEASE.jar
spring-tx-4.1.0.RELEASE.jar
spring-web-4.1.0.RELEASE.jar
spring-webmvc-4.1.0.RELEASE.jar
spring-webmvc-portlet-4.1.0.RELEASE.jar
dao层的注入
@Repository("bizDao")
public interfaceBizDao {
public SuservalidateUser(HashMap<String, String> map);
}
mybatis的配置
<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEmapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mappernamespace="com.accp.dao.BizDao">
<selectid="validateUser"parameterType="java.util.HashMap"resultType="com.accp.pojo.Suser">
select* from suser where sname=#{name} and spwd=#{pwd}
</select>
</ mapper >
service层的配置
@Service("bs")
public class BizServiceImpl implementsBizService{
//自动注解的方式来装配
@Autowired
privateBizDao bizdao;
publicBizDao getBizdao() {
returnbizdao;
}
publicvoid setBizdao(BizDao bizdao) {
this.bizdao= bizdao;
}
@Override
publicSuser validateUser(HashMap<String, String> map) {
//TODO Auto-generated method stub
System.out.println("服务层:11111");
returnbizdao.validateUser(map);
}
}
//接口继承接口,初期可以这么写用于学习和理解
service层
public interface BizService extendsBizDao{
}
/**
* 控制器处理model和view
* */、
Controller层
@Controller
public class BizAction {
@Autowired
privateBizService bs;
publicBizService getBs() {
returnbs;
}
publicvoid setBs(BizService bs) {
this.bs= bs;
}
@RequestMapping("/login.action")
publicString executionaction(HttpServletRequest request,HttpServletResponse response)throws Exception{
request.setCharacterEncoding("utf-8");
//获取index.jsp页面的请求过来的数据
Stringuname= request.getParameter("uname");
Stringupwd=request.getParameter("upwd");
System.out.println("用户名:"+uname);
System.out.println("密码:"+upwd);
HashMap<String,String> map=new HashMap<String,String>();//jdk提供
map.put("name",uname);
map.put("pwd",upwd);
Suseruser= bs.validateUser(map);
//System.out.println("获取后台的:"+user.getSname()+"\t"+user.getSpwd());
if(user!=null){
return"redirect:show.jsp";
}else{
return"redirect:index.jsp";
}
}
applicationContext-dao.xml配置
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 加载配置文件 -->
<context:property-placeholderlocation="classpath:database.properties"/>
<!-- 数据库连接池 -->
<beanid="dataSource"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="10"/>
<propertyname="maxIdle"value="5"/>
</bean>
<!-- mapper配置 -->
<!-- 让spring管理sqlsessionfactory使用mybatis和spring整合包中的 -->
<beanid="sqlSessionFactory"class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<propertyname="dataSource"ref="dataSource"/>
<!-- 加载mybatis的全局配置文件 -->
<propertyname="configLocation"value="classpath:SqlMapConfig.xml"/>
</bean>
<!--
MapperScannerConfigurer根据mapper自动生成id,这个id是类名的首字母小写
-->
<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
<propertyname="basePackage"value="com.accp.dao"/>
<!--
value="sqlSessionFactory":注入的是 sqlSessionFactory
name="sqlSessionFactoryBeanName"是将sqlSessionFactory注入到com.accp.dao包里的所有接口里面,所以是根据sqlSessionFactoryBeanName
-->
<propertyname="sqlSessionFactoryBeanName"value="sqlSessionFactory"></property>
</bean>
</beans>
applicationContext-service配置
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 注册服务层 -->
<beanid="bs"class="com.accp.service.impl.BizServiceImpl">
</bean>
</beans>
applicationContext.xml配置
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-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/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">
<!-- 要扫描的东东用注解-->
<context:component-scanbase-package="com.accp.web"></context:component-scan>
<!-- 接口对应xml -->
<!-- 使用mvc:annotation-driven代替上边注解映射器和注解适配器配置
mvc:annotation-driven默认加载很多的参数绑定方法,
比如json转换解析器就默认加载了,如果使用mvc:annotation-driven不用配置上边的RequestMappingHandlerMapping和RequestMappingHandlerAdapter
实际开发时使用mvc:annotation-driven
-->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器 前缀和 后缀
解析jsp解析,默认使用jstl标签,classpath下的得有jstl的包
-->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver"></bean>
</ beans >
SqlMapconfig配置
<?xmlversion="1.0"encoding="UTF-8"?> <!DOCTYPEconfiguration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration>
</configuration> |
Datebase.properties配置
jdbc.driver=oracle.jdbc.driver.OracleDriver jdbc.url=jdbc\:oracle\:thin\:@localhost\:1521\:orcl jdbc.username=j1025 jdbc.password=j1025 |
Log4j配置
# Rules reminder: # DEBUG < INFO < WARN < ERROR < FATAL
# Global logging configuration log4j.rootLogger=INFO,stdout
# My logging configuration... log4j.logger.cn.jbit.mybatisdemo=DEBUG
## Console output... log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p%d %C: %m%n
log4j.logger.org.apache.ibatis=DEBUG ## log4j.logger.org.apache.jdbc.SimpleDataSource=DEBUG log4j.logger.org.apache.ibatis.jdbc.ScriptRunner=DEBUG ## log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapclientDelegate=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
|
Web.xml配置
<?xmlversion="1.0"encoding="UTF-8"?> <web-appxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://java.sun.com/xml/ns/javaee"xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"id="WebApp_ID"version="2.5"> <display-name>Demo_dt37_ssm_01</display-name> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!-- spring mvc 旧一点(新的)的版本配置servlet servlet-mapping 在src下面的applicationContext-xxx.xml配置文件如何加载? --> <context-param> <param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext-*.xml</param-value>
</context-param>
<servlet> <servlet-name>dispatcherServlet</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> </servlet> <servlet-mapping> <servlet-name>dispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list>
</web-app>
|