SSM整合
程序员文章站
2024-03-24 17:37:10
...
记录一下整合的过程:
一、整合
4个配置文件。jdbc.properties、applicationContext.xml、spring-mvc.xml、web.xml
首先是jdbc.properties。
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc\:sqlserver\://127.0.0.1\:1433;databaseName\=blog
jdbc.username=sa
jdbc.password=123
然后applicationContext.xml,这是一个spring的配置文件,主要负责配置数据源,事务管理,以及对Spring Mybatis进行整合。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 加载属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 配置数据库连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 配置事务管理器 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven transaction-manager="txManager"/>
<!-- 扫描生成bean 服务类-->
<context:component-scan base-package="com.mao.service"></context:component-scan>
<!-- mybatis-spring整合 start -->
<!-- 配置SqlSessionFactoryBean -->
<bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 别名包扫描 com.mao.entity这个包下面的实体可以使用别名-->
<property name="typeAliasesPackage" value="com.mao.entity"></property>
</bean>
<!-- 映射文件扫描配置 -->
<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描哪个包 -->
<property name="basePackage" value="com.mao.dao"></property>
</bean>
<!-- mybatis-spring整合 end -->
</beans>
spring-mvc.xml 这是SpringMVC的配置文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!-- 使用注解驱动 -->
<mvc:annotation-driven />
<!-- 扫描哪个包,扫描到@Controller注解的类就知道它是一个控制器,同时将其纳入spring ioc容器便于管理 -->
<context:component-scan base-package="com.mao.controler"></context:component-scan>
<!-- 视图解析器 -->
<bean id="viewResolver"
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: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">
<display-name>MyBlog</display-name>
<!-- 配置ContextLoaderListener用以初始化spring ioc容器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置Spring IOC配置文件的路径 这样Spring就能找到applicationContext.xml配置文件并去加载他们 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- 启动spring MVC 所有的请求都是先经过这个核心控制器 -->
<servlet>
<servlet-name>DispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 默认会加载一个配置文件 修改配置的路径让其加载spring-mvc.xml -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<!-- 让这个控制器拦截所有.do结尾的请求 -->
<servlet-name>DispatcherServlet</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
这样ssm就已经整合完成了。
二、测试
编写代码前先看下整体的结构。
首先是映射文件:一个接口(BlogMapper.java)和对应的XML文件(BlogMapper.xml)需要注意名称应该相同。
public interface BlogMapper {
public void save(Blog blog);
}
BlogMapper.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.mao.dao.BlogMapper">
<insert id="save" parameterType="blog">
insert into blog(title,author,addtime,content)
values(#{title},#{author},#{addtime},#{content})
</insert>
</mapper>
实体类
@Alias("blog")
public class Blog implements Serializable{
private String title;
private String author;
private Date addtime;
private String content;
//get、set省略
}
服务接口的实现类
@Service
public class BlogServiceImpl implements BlogService{
//注入BlogMapper
@Autowired
BlogMapper blogMapper;
@Transactional//事务
@Override
public void save(Blog blog) {
blogMapper.save(blog);
}
}
控制器BlogController.java
@Controller
@RequestMapping("/blog")
public class BlogController {
//注入服务类
@Autowired
BlogService blogService;
@RequestMapping("/save.do")
public String save(){
Blog blog=new Blog();
blog.setTitle("Android");
blog.setAuthor("张三");
blog.setAddtime(new Date());
blog.setContent("你好");
blogService.save(blog);
return "success";
}
}
这样就可以访问http://localhost:8080/MyBlog/blog/save.do 保存数据后成功跳转到/WEB-INF/jsp/success.jsp
最后jar包。
上一篇: 安装以及配置Maven
下一篇: Maven仓库安装配置及使用
推荐阅读
-
SSM整合
-
整合SSM
-
SSM(spring、springMVC、mybatis) 三大框架集成(注解版)
-
spring-springmvc-mybatis三大框架整合
-
SSM整合
-
IntelliJ IDEA创建Spring SpringMVC MyBatis整合Maven项目,并提交至Github
-
Spring Boot整合Kafka的简单用例(@KafkaListener注解)
-
SpringBoot整合Mybatis和MySql注意事项
-
ExtJS5 整合Spring4之二 博客分类: ExtJSjava ExtJSgridRESTful
-
SpringMVC+mybatis+DWR3注解 博客分类: 框架整合Spring springMVC注解SpringMVC+mybatis+DWR3SpringMVC+DWR3注解配置框架整合