Skip to content

Commit 0ff5841

Browse files
committed
spring、mybatis和springmvc整合
1 parent 3cb327a commit 0ff5841

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+3558
-18
lines changed

.classpath

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="src" path="config"/>
5+
<classpathentry kind="src" path="test"/>
56
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6">
67
<attributes>
78
<attribute name="owner.project.facets" value="java"/>
@@ -14,21 +15,14 @@
1415
</classpathentry>
1516
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.web.container"/>
1617
<classpathentry kind="con" path="org.eclipse.jst.j2ee.internal.module.container"/>
17-
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/aopalliance-1.0.jar"/>
18-
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/aspectj-1.7.1.jar"/>
19-
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/aspectjweaver-1.7.1.jar"/>
2018
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/commons-logging-1.2.jar"/>
2119
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/jstl-1.2.jar"/>
22-
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-aop-4.2.4.RELEASE.jar"/>
2320
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-beans-4.2.4.RELEASE.jar"/>
2421
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-context-4.2.4.RELEASE.jar"/>
2522
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-core-4.2.4.RELEASE.jar"/>
2623
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-expression-4.2.4.RELEASE.jar"/>
27-
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-jdbc-4.2.4.RELEASE.jar"/>
28-
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-orm-4.2.4.RELEASE.jar"/>
2924
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-test-4.2.4.RELEASE.jar"/>
30-
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-tx-4.2.4.RELEASE.jar"/>
31-
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-web-4.2.4.RELEASE.jar"/>
32-
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/spring-webmvc-4.2.4.RELEASE.jar"/>
25+
<classpathentry kind="lib" path="WebRoot/WEB-INF/lib/asm-4.2.jar"/>
26+
<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>
3327
<classpathentry kind="output" path="WebRoot/WEB-INF/classes"/>
3428
</classpath>

.settings/org.eclipse.wst.common.component

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<wb-resource deploy-path="/" source-path="/WebRoot" tag="defaultRootSource"/>
44
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
55
<wb-resource deploy-path="/WEB-INF/classes" source-path="/config"/>
6+
<wb-resource deploy-path="/WEB-INF/classes" source-path="/test"/>
67
<property name="context-root" value="SpringMVC_Study"/>
78
<property name="java-output-path" value="/SpringMVC_Study/WebRoot/WEB-INF/classes"/>
89
</wb-module>
-434 Bytes
Binary file not shown.

WebRoot/WEB-INF/lib/asm-4.2.jar

46.8 KB
Binary file not shown.

WebRoot/WEB-INF/lib/cglib-3.1.jar

276 KB
Binary file not shown.
119 KB
Binary file not shown.
60.6 KB
Binary file not shown.
695 KB
Binary file not shown.

WebRoot/WEB-INF/lib/log4j-1.2.17.jar

478 KB
Binary file not shown.

WebRoot/WEB-INF/lib/log4j-api-2.2.jar

130 KB
Binary file not shown.
807 KB
Binary file not shown.

WebRoot/WEB-INF/lib/mybatis-3.3.0.jar

1.35 MB
Binary file not shown.
47.6 KB
Binary file not shown.
Binary file not shown.
31.4 KB
Binary file not shown.
8.65 KB
Binary file not shown.

WebRoot/WEB-INF/web.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,16 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<web-app xmlns: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_3_0.xsd" id="WebApp_ID" version="3.0">
33
<display-name>SpringMVC_Study</display-name>
4+
5+
<!-- 加载spring容器 -->
6+
<context-param>
7+
<param-name>contextConfigLocation</param-name>
8+
<param-value>classpath:spring/applicationContext-*.xml</param-value>
9+
</context-param>
10+
<listener>
11+
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
12+
</listener>
13+
414
<welcome-file-list>
515
<welcome-file>index.jsp</welcome-file>
616
</welcome-file-list>

config/db.properties

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
jdbc.driver=com.mysql.jdbc.Driver
2+
jdbc.url=jdbc\:mysql\://localhost\:3306/mybatis
3+
jdbc.username=root
4+
jdbc.password=root

config/log4j.properties

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Global logging configuration
2+
# developer-->DEBUG productor-->INFO or ERROR
3+
log4j.rootLogger=DEBUG, stdout
4+
# MyBatis logging configuration...
5+
log4j.logger.org.mybatis.example.BlogMapper=TRACE
6+
# Console output...
7+
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
8+
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
9+
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

config/mybatis/sqlMapConfig.xml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
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+
7+
<!-- setting配置根据需要再添加 -->
8+
<!-- 配置别名 -->
9+
<typeAliases>
10+
<package name="ssm.po"/>
11+
</typeAliases>
12+
13+
<!-- mapper这里不需要配置了,因为跟spring整合后,在spring那边会进行mapper的扫描
14+
但必须遵循:mapper.xml和mapper.java必须同名且在一个目录
15+
-->
16+
</configuration>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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:mvc="http://www.springframework.org/schema/mvc"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xmlns:aop="http://www.springframework.org/schema/aop"
7+
xmlns:tx="http://www.springframework.org/schema/tx"
8+
xsi:schemaLocation="http://www.springframework.org/schema/beans
9+
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
10+
http://www.springframework.org/schema/mvc
11+
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
12+
http://www.springframework.org/schema/context
13+
http://www.springframework.org/schema/context/spring-context.xsd
14+
http://www.springframework.org/schema/aop
15+
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
16+
http://www.springframework.org/schema/tx
17+
http://www.springframework.org/schema/tx/spring-tx.xsd">
18+
19+
<!-- 加载db.properties文件中的内容 -->
20+
<context:property-placeholder location="classpath:db.properties"/>
21+
22+
<!-- 配置数据源dbcp -->
23+
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
24+
<property name="driverClassName" value="${jdbc.driver}" />
25+
<property name="url" value="${jdbc.url}" />
26+
<property name="username" value="${jdbc.username}" />
27+
<property name="password" value="${jdbc.password}" />
28+
<property name="maxActive" value="10" />
29+
<property name="maxIdle" value="5" />
30+
</bean>
31+
32+
<!-- 配置sqlSessionFactory -->
33+
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
34+
<property name="dataSource" ref="dataSource" />
35+
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"/>
36+
</bean>
37+
38+
<!-- 配置mapper扫描器 -->
39+
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
40+
<!-- 扫描包的路径,如果需要扫描多个包,中间使用半角 逗号隔开-->
41+
<property name="basePackage" value="ssm.mapper"/>
42+
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
43+
</bean>
44+
45+
</beans>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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:mvc="http://www.springframework.org/schema/mvc"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xmlns:aop="http://www.springframework.org/schema/aop"
7+
xmlns:tx="http://www.springframework.org/schema/tx"
8+
xsi:schemaLocation="http://www.springframework.org/schema/beans
9+
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
10+
http://www.springframework.org/schema/mvc
11+
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
12+
http://www.springframework.org/schema/context
13+
http://www.springframework.org/schema/context/spring-context.xsd
14+
http://www.springframework.org/schema/aop
15+
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
16+
http://www.springframework.org/schema/tx
17+
http://www.springframework.org/schema/tx/spring-tx.xsd">
18+
19+
<!-- 商品管理的service -->
20+
<bean id="itemsService" class="ssm.service.impl.ItemsServiceImpl"/>
21+
22+
</beans>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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:mvc="http://www.springframework.org/schema/mvc"
5+
xmlns:context="http://www.springframework.org/schema/context"
6+
xmlns:aop="http://www.springframework.org/schema/aop"
7+
xmlns:tx="http://www.springframework.org/schema/tx"
8+
xsi:schemaLocation="http://www.springframework.org/schema/beans
9+
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
10+
http://www.springframework.org/schema/mvc
11+
http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd
12+
http://www.springframework.org/schema/context
13+
http://www.springframework.org/schema/context/spring-context.xsd
14+
http://www.springframework.org/schema/aop
15+
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
16+
http://www.springframework.org/schema/tx
17+
http://www.springframework.org/schema/tx/spring-tx.xsd">
18+
19+
<!-- 事务管理器 -->
20+
<!-- 对mybatis操作数据事务控制,spring使用jdbc的事务控制类 -->
21+
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
22+
<!-- 数据源dataSource在applicationContex-dao.xml中配置了 -->
23+
<property name="dataSource" ref="dataSource"/>
24+
</bean>
25+
26+
<!-- 通知 -->
27+
<tx:advice id="txAdvice" transaction-manager="transactionManager">
28+
<tx:attributes>
29+
<tx:method name="save*" propagation="REQUIRED"/>
30+
<tx:method name="delete*" propagation="REQUIRED"/>
31+
<tx:method name="update*" propagation="REQUIRED"/>
32+
<tx:method name="insert*" propagation="REQUIRED"/>
33+
<tx:method name="find*" propagation="SUPPORTS" read-only="true"/>
34+
<tx:method name="get*" propagation="SUPPORTS" read-only="true"/>
35+
<tx:method name="select*" propagation="SUPPORTS" read-only="true"/>
36+
</tx:attributes>
37+
</tx:advice>
38+
39+
<aop:config>
40+
<aop:advisor advice-ref="txAdvice" pointcut="execution(* ssm.service.impl.*.*(..))"/>
41+
</aop:config>
42+
43+
</beans>
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package ssm.controller;
2+
3+
import java.util.List;
4+
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.web.bind.annotation.RequestMapping;
8+
import org.springframework.web.servlet.ModelAndView;
9+
10+
import ssm.po.ItemsCustom;
11+
import ssm.service.ItemsService;
12+
13+
@Controller
14+
public class ItemsController {
15+
16+
@Autowired
17+
private ItemsService itemsService;
18+
19+
@RequestMapping("/queryItemsssm")
20+
public ModelAndView queryItems() throws Exception {
21+
22+
//调用service查找数据库,查询商品列表
23+
List<ItemsCustom> itemsList = itemsService.findItemsList(null);
24+
25+
//返回ModelAndView
26+
ModelAndView modelAndView = new ModelAndView();
27+
modelAndView.addObject("itemsList", itemsList);
28+
modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");
29+
30+
return modelAndView;
31+
}
32+
}

src/ssm/mapper/ItemsMapper.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package ssm.mapper;
2+
3+
import java.util.List;
4+
import org.apache.ibatis.annotations.Param;
5+
import ssm.po.Items;
6+
import ssm.po.ItemsExample;
7+
8+
public interface ItemsMapper {
9+
int countByExample(ItemsExample example);
10+
11+
int deleteByExample(ItemsExample example);
12+
13+
int deleteByPrimaryKey(Integer id);
14+
15+
int insert(Items record);
16+
17+
int insertSelective(Items record);
18+
19+
List<Items> selectByExampleWithBLOBs(ItemsExample example);
20+
21+
List<Items> selectByExample(ItemsExample example);
22+
23+
Items selectByPrimaryKey(Integer id);
24+
25+
int updateByExampleSelective(@Param("record") Items record, @Param("example") ItemsExample example);
26+
27+
int updateByExampleWithBLOBs(@Param("record") Items record, @Param("example") ItemsExample example);
28+
29+
int updateByExample(@Param("record") Items record, @Param("example") ItemsExample example);
30+
31+
int updateByPrimaryKeySelective(Items record);
32+
33+
int updateByPrimaryKeyWithBLOBs(Items record);
34+
35+
int updateByPrimaryKey(Items record);
36+
}

0 commit comments

Comments
 (0)