spring+springmvc+mybatis框架的搭建
2016-06-21 22:35
579 查看
总结一下SSM框架的搭建,先给大家说一下整个框架搭建的思路和步骤:
Mybatis版本:Mybatis3.2.7
jar包:
1.数据库驱动包
2.Mybatis的jar包
3.Mybatis和spring整合包
4.Log4j包
5.数据库连接池包:dbcp包
6.spring3.2的所有jar包
7.jstl包
以下是是项目需要的所有jar包:
这是整个项目的目录:
第一步:整合dao层
Mybatis和spring整合,通过spring管理mapper接口。 使用mapper的扫描器自动扫描mapper接口在spring中进行注册。
第二步:整合service层
通过spring管理service接口 使用配置方式将service接口配置在spring配置文件中。 实现事务控制。
第三步:整合springmvc
由于springmvc是spring的模块,不需要整合。环境准备
Springmvc版本:spring3.2Mybatis版本:Mybatis3.2.7
jar包:
1.数据库驱动包
2.Mybatis的jar包
3.Mybatis和spring整合包
4.Log4j包
5.数据库连接池包:dbcp包
6.spring3.2的所有jar包
7.jstl包
以下是是项目需要的所有jar包:
这是整个项目的目录:
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!-- 全局setting配置,根据需要再加 --> <!-- 配置别名 批量扫描--> <typeAliases> <package name="com.li.mapper"/> </typeAliases> </configuration>
db.properties
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/mybatis jdbc.username=root jdbc.password=123456
log4j.properties
log4j.rootLogger=DEBUG,stdout log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
springmvc.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd" default-autowire="byName"> <!-- 非注解配置Handler --> <!-- <bean id="userContorller" class="com.li.controller.UserContorller"/> --> <!-- 非注解处理器映射器 --> <!-- <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"> </bean> --> <!-- 非注解处理器适配器 --> <!-- <bean class="org.springframework.web.servlet.mvc.SimpleFormController"/> --> <!-- 另一个非注解的适配器 --> <!-- <bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter"/> --> <!-- 简单url映射 集中配置url映射--> <!-- <bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> <property name="mappings"> <props> <prop key="/test.action">userContorller</prop> </props> </property> </bean> --> <!-- 注解Handler可以单个配置,建议使用组件扫描--> <context:component-scan base-package="com.li.controller" /> <!-- 配置注解的处理器映射器和适配器 --> <mvc:annotation-driven /> <!-- 视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <!-- <property name="prefix" value="/"/>路径前缀 <property name="suffix" value=".jsp"/>路径后缀 --> </bean> </beans>
applicationContext-dao.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <!-- 加载db.properties中的内容 --> <context:property-placeholder location="classpath:db.properties"/> <!-- 配置数据源,dbcp --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/mybatis"/> <property name="username" value="root"/> <property name="password" value="123456"/> <property name="maxActive" value="30"/> <property name="maxIdle" value="5"/> </bean> <!-- sqlSesseionFactory --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载mybatis的全局配置文件 --> <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/> <!-- 数据库连接池 --> <property name="dataSource" ref="dataSource"/> </bean> <!-- mapper接口和mapper映射文件的扫描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 扫描包路径 --> <property name="basePackage" value="com.li.mapper"></property> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property> </bean> </beans>
applicationContext-service.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <!-- 定义商品管理的service --> <bean id="userImpi" class="com.li.service.impi.UserImpi"/> </beans>
applicationContext-transaction.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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName"> <!-- 事务管理器 使用jdbc的事务控制类--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 数据源 --> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 通知 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!-- 传播行为 --> <tx:method name="save*" propagation="REQUIRED"/> <tx:method name="delete*" propagation="REQUIRED"/> <tx:method name="insert*" propagation="REQUIRED"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="find*" propagation="SUPPORTS" read-only="true"/> <tx:method name="get*" propagation="SUPPORTS" read-only="true"/> <tx:method name="select*" propagation="SUPPORTS" read-only="true"/> </tx:attributes> </tx:advice> <!-- aop调用通知的 --> <aop:config> <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.li.service.impi.*.*(..))"/> </aop:config> </beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/applicationContext-*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/springmvc.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>springmvc</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> </web-app>
UserMapper.java
package com.li.mapper; import com.li.pojo.User; public interface UserMapper { //查询测试 public User findUserById(int id)throws Exception; }
UserMapper.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.li.mapper.UserMapper"> <select id="findUserById" parameterType="int" resultType="com.li.pojo.User"> SELECT * FROM user WHERE id=#{value} </select> </mapper>
User.java
package com.li.pojo; public class User { private String username; private String addres; private int id; private String sex; private String birthday; public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getAddres() { return addres; } public void setAddres(String addres) { this.addres = addres; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getBirthday() { return birthday; } public void setBirthday(String birthday) { this.birthday = birthday; } }
UserInterface.java
package com.li.service; import com.li.pojo.User; public interface UserInterface { public User findUserById(int id)throws Exception; }
UserImpi.java
package com.li.service.impi; import org.springframework.beans.factory.annotation.Autowired; import com.li.mapper.UserMapper; import com.li.pojo.User; import com.li.service.UserInterface; public class UserImpi implements UserInterface{ //自动注入 @Autowired private UserMapper userMapper; @Override public User findUserById(int id) throws Exception { // TODO Auto-generated method stub return userMapper.findUserById(id); } }
UserController.java
package com.li.controller; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.ModelAndView; import com.li.pojo.User; import com.li.service.UserInterface; @Controller public class UserContorller{ @Autowired private UserInterface userInterface; @RequestMapping("/test") public ModelAndView test() throws Exception{ User u = userInterface.findUserById(3); ModelAndView mav = new ModelAndView(); //相当于request的etAttribute方法 mav.addObject("user", u); //指定视图 mav.setViewName("/index.jsp"); return mav; } }
相关文章推荐
- 《第一行代码》-1.4.添加LogCat到eclipse及初步使用LogCat
- Quartz+spring
- Spring Security入门Demo
- java线程协作,经典生产者/消费者模式(二、Lock和Condition)
- Java - PAT - 1001. A+B Format (20)
- java可变参数
- Struts2系列:(3)Action的访问路径和缺省后缀
- springmvc+mybatis批量上传文件
- Struts2常用标签总结
- MyEclipse和IntelliJ IDEA安装使用SVN
- Spring实例化的3种方式
- Java四种线程池的使用
- Eclipse NDK 开发之 HelloWorld
- java.lang.IllegalThreadStateException异常原因解析
- java学习之路---常用类
- Java 中基本类型和字符串之间的转换
- struts2框架 初始别
- java 成神之路 (四)
- 《疯狂JAVA讲义》——聊天系统(2)
- Java 设计模式之 简单工厂模式