ssm整合
spring、springmvc、Mybatis

applicationContext.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: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.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--配置包扫描-->
<context:component-scan base-package="cn.tedu"/>
<!--配置注解方式DI-->
<context:annotation-config/>
<!--配置注解方式AOP-->
<aop:aspectj-autoproxy/>
<!--配置数据源-->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="jdbcUrl" value="jdbc:mysql:///ssm05"/>
<property name="user" value="root"/>
<property name="password" value="root"/>
</bean>
<!--整合MyBatis-->
<!--配置SqlSessionFactory工厂-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:sqlMapConfig.xml"/>
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:/mappers/*.xml"/>
</bean>
<!--配置MyBatis的MapperBean扫描器,负责为MapperBean生成实现类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.tedu.mapper"/>
</bean>
<!--配置声明式事务处理-->
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--开启声明式事务处理-->
<tx:annotation-driven/>
</beans>
springmvc.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:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--配置包扫描-->
<context:component-scan base-package="cn.tedu.controller"/>
<!--注解方式mvc-->
<mvc:annotation-driven/>
<!--配置视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
</beans>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--配置监听器,管理Spring容器的生命周期-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/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>
<!--配置springmvc配置文件-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/springmvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>
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>
</configuration>
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="cn.tedu.mapper.UserMapper">
<select id="selectById" resultType="cn.tedu.domain.User">
select * from user where id = #{id};
</select>
<select id="selectBetweenAge" resultType="cn.tedu.domain.User">
select * from user where age between #{min} and #{max};
</select>
<insert id="insertUser">
insert into user values (null,#{name},#{age});
</insert>
<delete id="delUserById">
delete from user where id = #{id};
</delete>
</mapper>
User
package cn.tedu.domain;
public class User {
private int id;
private String name;
private int age;
public User() {
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
UserMapper
public interface UserMapper {
public List<User> selectById(int id);
public List<User> selectBetweenAge(@Param("min") int min, @Param("max") int max);
public int insertUser(User user);
public int delUserById(int id);
}
TestService
public interface TestService {
public void test01();
}
TestServiceImpl
@Service
public class TestServiceImpl implements TestService {
@Autowired
private UserMapper userMapper = null;
@Transactional
@Override
public void test01() {
System.out.println("TestService..test01..");
userMapper.insertUser(new User(9,"zzz",798));
int i = 1/0;
}
}
MyController01
@Controller
@RequestMapping("/my01")
public class MyController01 {
@Autowired
private TestService service = null;
@RequestMapping("/test01.action")
public String test01(){
System.out.println("my01test01.action..");
service.test01();
return "my01test01";
}
}


本文档展示了如何整合Spring、SpringMVC和MyBatis,并配置数据源、SqlSessionFactory、MapperScannerConfigurer以及声明式事务处理。示例中包含了web.xml、applicationContext.xml和springmvc.xml的配置,以及UserMapper接口和实体类User的定义,演示了数据库操作和事务管理的实现。
1129

被折叠的 条评论
为什么被折叠?



