applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 开启IOC扫描 -->
<context:component-scan
base-package="com.cn.service">
</context:component-scan>
<!-- 配置数据源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass"
value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl"
value="jdbc:mysql://localhost:3306/test"/>
<property name="user" value="root" />
<property name="password" value="1234" />
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 关联连接池 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 加载sql映射文件 -->
<property name="mapperLocations"
value="classpath:com/cn/dao/UserMapper.xml"></property>
</bean>
<!-- Mapper接口扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cn.dao"></property>
</bean>
<!-- 开启Spring事务 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 开启事务注解 -->
<tx:annotation-driven
transaction-manager="txManager" />
</beans>
springmvc-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc ">
<!-- 扫描Controller -->
<context:component-scan
base-package="
com.cn.controller ">
</context:component-scan>
<!-- 注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 视图解析器:简化在Controller类编写的视图路径 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 前缀 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀 -->
<property name="suffix" value=".jsp"/>
</bean>
</beans>
然后就是代码的编写,这里举一个简单的例子
controller
@Controller
@RequestMapping
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login")
public String golog() {
return "login";
}
@RequestMapping("/goreg")
public String goreg() {
return "register";
}
@RequestMapping("/register")
public String login(User user) {
userService.saveUser(user);
return "succ";
}
@RequestMapping("/login")
public String register(String name,String pwd) {
User user=userService.findUser(name, pwd);
if(user!=null) {
return "succ";
}else {
return "fail";
}
}
}
dao
UserMapper
这里要注意加注解,要不然spring将无法管理
@Repository
@Mapper
public interface UserMapper {
void saveUser(User user);
User findUser(@Param("name")String name,@Param("pwd")String pwd);
}
UserMapper.xml
这里要注意sql语句的优化,这里是没有优化的,就简单举例,查询语句应该指明查询的字段
<?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.cn.dao.UserMapper">
<!-- saveUser()方法实现 -->
<insert id="saveUser" parameterType="com.cn.entiy.User">
insert into test (name,pwd) values (#{name},#{pwd})
</insert>
<!-- findUser()方法实现 -->
<select id="findUser" resultType="com.cn.entiy.User">
select * from test where name=#{name} and pwd=#{pwd}
</select>
</mapper>
entiy或者pojo层(省略)
service
UserService
public interface UserService {
void saveUser(User user);
User findUser(String name, String pwd);
}
UserServcieImpl
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {
@Autowired
private UserMapper userMapepr;
@Override
public void saveUser(User user) {
// TODO Auto-generated method stub
userMapepr.saveUser(user);
}
@Override
public User findUser(String name, String pwd) {
// TODO Auto-generated method stub
return userMapepr.findUser(name, pwd);
}
}
web.xml
org.springframework.web.servlet.DispatcherServlet需要去重点理解这个类
<?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">
<!-- DispatcherServlet-->
<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:applicationContext.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- <!– 乱码过滤–>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>-->
<!-- session-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
</web-app>
总结:在SSM框架中,我们除了配置文件要去理解透彻,它是怎么将业务等交于spring管理的,考虑框架的优缺点,比如:优点有将业务等交由spring管理;缺点:配置文件过于复杂,所以我们后来可以用springboot来解决这一问题
此项目只是一个大概流程,功能些还需要去完善,感谢!!!
原文:https://www.cnblogs.com/haotian6/p/14798652.html