点这里看实际效果! 账密:admin 123456
框架介绍:
主角即Spring、SpringMVC、MyBatis、easyUI,大家应该也都有所了解,概念性的东西就不写了,有万能的百度。
工作环境:
jdk 1.7
mysql 5.6
IntelliJ IDEA 15
tomcat 7
以上为本地的构建环境,当然,如果开发环境版本或者ide与以上不同也是可以正常搭建环境的,比如你jdk是1.8啊,ide用的是eclipse或者myeclipse都是完全OK的。
整合步骤:
以下为项目的目录结构,
entity包中即为本项目的所有实体类,dao包中为数据层接口,mappers中为数据层接口的sql映射文件,运行的sql语句是在这些文件中定义的,admin包中则是控制器,以上包名都可以按照个人习惯来命名,WebRoot中则是jsp文件及jar包等文件。
首先第一步,新建一个javaWeb项目,接下来在src目录下entity包、mappers包、dao包和service包,并新增配置Spring的文件applicationContext.xml和mybatis的配置文件mybatis-config.xml。
1、applicationContext.xml:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:aop="http://www.springframework.org/schema/aop"
5 xmlns:context="http://www.springframework.org/schema/context"
6 xmlns:tx="http://www.springframework.org/schema/tx"
7 xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
8 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
9 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
11
12 <!-- 自动扫描dao层和service层,将包中的类纳入到Spring的管理中 -->
13 <context:component-scan base-package="com.core.dao"/>
14 <context:component-scan base-package="com.core.service"/>
15
16 <!-- 配置数据源 -->
17 <bean id="dataSource"
18 class="org.springframework.jdbc.datasource.DriverManagerDataSource">
19 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
20 <property name=“url" value="jdbc:mysql://xx.xx.xx.xx:xx/db"/>
21 <!-- 改为你的地址即可 -->
22 <property name="username" value="xxxx"/>
23 <property name="password" value="xxxx"/>
24 </bean>
25
26
27 <!-- 配置mybatis的sqlSessionFactory -->
28 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
29 <property name="dataSource" ref="dataSource"/>
30 <!-- 自动扫描mappers包下的所有xml文件 -->
31 <property name="mapperLocations" value="classpath:com/core/mappers/*.xml"></property>
32 <!-- mybatis配置文件 -->
33 <property name="configLocation" value="classpath:mybatis-config.xml"></property>
34 </bean>
35
36 <!-- dao接口所在包名,Spring会自动查找其下的类 -->
37 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
38 <property name="basePackage" value="com.core.dao"/>
39 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
40 </bean>
41
42 <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
43 <bean id="transactionManager"
44 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
45 <property name="dataSource" ref="dataSource"/>
46 </bean>
47
48 <!-- 配置事务通知属性 -->
49 <tx:advice id="txAdvice" transaction-manager="transactionManager">
50 <!-- 定义事务传播属性 -->
51 <tx:attributes>
52 <tx:method name="insert*" propagation="REQUIRED"/>
53 <tx:method name="update*" propagation="REQUIRED"/>
54 <tx:method name="upd*" propagation="REQUIRED"/>
55 <tx:method name="edit*" propagation="REQUIRED"/>
56 <tx:method name="save*" propagation="REQUIRED"/>
57 <tx:method name="add*" propagation="REQUIRED"/>
58 <tx:method name="new*" propagation="REQUIRED"/>
59 <tx:method name="set*" propagation="REQUIRED"/>
60 <tx:method name="remove*" propagation="REQUIRED"/>
61 <tx:method name="delete*" propagation="REQUIRED"/>
62 <tx:method name="del*" propagation="REQUIRED"/>
63 <tx:method name="change*" propagation="REQUIRED"/>
64 <tx:method name="check*" propagation="REQUIRED"/>
65 <tx:method name="get*" propagation="REQUIRED" read-only="true"/>
66 <tx:method name="search*" propagation="REQUIRED" read-only="true"/>
67 <tx:method name="find*" propagation="REQUIRED" read-only="true"/>
68 <tx:method name="load*" propagation="REQUIRED" read-only="true"/>
69 <tx:method name="*" propagation="REQUIRED" read-only="true"/>
70 </tx:attributes>
71 </tx:advice>
72
73 <!-- 配置事务切面,service包中的类会被代理 -->
74 <aop:config>
75 <aop:pointcut id="serviceOperation"
76 expression="(execution(* com.core.service.*.*(..)))"/>
77 <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation"/>
78 </aop:config>
79
80 </beans>
2、mybatis-config.xml
1 <?xml version="1.0" encoding="UTF-8" ?>
2 <!DOCTYPE configuration
3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
5 <configuration>
6 <settings>
7 <setting name="logImpl" value="LOG4J"/>
8 </settings>
9 <typeAliases>
10 <package name="com.core.entity"/>
11 </typeAliases>
12 </configuration>
3、接着是SpringMVC的配置文件:
1 <?xml version="1.0" encoding="UTF-8"?>
2 <beans xmlns="http://www.springframework.org/schema/beans"
3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:context="http://www.springframework.org/schema/context"
5 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
7
8 <!-- 扫描admin包,将控制器纳入Spring的管理中 -->
9 <context:component-scan base-package="com.core.admin"/>
10
11 <!-- 视图解析器,视图模式下的前缀和后缀 -->
12 <bean id="viewResolver"
13 class="org.springframework.web.servlet.view.InternalResourceViewResolver">
14 <property name="prefix" value=“/“/>
15 <property name="suffix" value=".jsp"></property>
16 </bean>
17
18 </beans>
4、web.xml文件
1 <?xml version="1.0" encoding="UTF-8"?>
2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
4 id="WebApp_ID" version="2.5">
5 <display-name>ssm-demo</display-name>
6 <welcome-file-list>
7 <welcome-file>index.html</welcome-file>
8 </welcome-file-list>
9 <context-param>
10 <param-name>contextConfigLocation</param-name>
11 <param-value>classpath:applicationContext.xml</param-value>
12 </context-param>
13
14 <filter>
15 <filter-name>encodingFilter</filter-name>
16 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
17 <init-param>
18 <param-name>encoding</param-name>
19 <param-value>UTF-8</param-value>
20 </init-param>
21 </filter>
22 <filter-mapping>
23 <filter-name>encodingFilter</filter-name>
24 <url-pattern>/*</url-pattern>
25 </filter-mapping>
26
27
28 <!-- spring请求配置,指向springmvc的核心配置文件,定义所有以.do结尾的请求都被springmvc拦截 -->
29 <servlet>
30 <servlet-name>springMVC</servlet-name>
31 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
32 <init-param>
33 <param-name>contextConfigLocation</param-name>
34 <param-value>classpath:spring-mvc.xml</param-value>
35 </init-param>
36 <!--加载顺序为1 -->
37 <load-on-startup>1</load-on-startup>
38 </servlet>
39 <listener>
40 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
41 </listener>
42 <servlet-mapping>
43 <servlet-name>springMVC</servlet-name>
44 <url-pattern>*.do</url-pattern>
45 </servlet-mapping>
46
47 <!— 错误页面配置,这里只是简单的配置了一下 -->
48 <error-page>
49 <error-code>404</error-code>
50 <location>/main.jsp</location>
51 </error-page>
52
53 <error-page>
54 <error-code>500</error-code>
55 <location>/main.jsp</location>
56 </error-page>
57 </web-app>
整合过程中需要注意的点:
项目编码设置正确,jar包要齐全,mysql要能正常登录。
当然,也可以直接导入源码,
点击这里下载代码
github地址
在这里
需要帮助的话,可以留言。