SSM框架集成的搭建
在此博主为大家记录配置Spring,Spring MVC,以及Mybaties三个框架的流程
总共需要分以下几步
引入所需Jar包
博主的文件结构是
编写web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>SprigspringMybaties1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<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:springmvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 现在配置spring的servlet映射 -->
<servlet-mapping>
<servlet-name>SpringMVC</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<!--这个路径代表src路径下的applicationContext.xml-->
<param-value>WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
</web-app>
编写实体类User以及UserMapper.xml
实体类User:
public class User {
private int id;
private String username;
private String password;
private String money;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getMoney() {
return money;
}
public void setMoney(String money) {
this.money = money;
}
}
映射文件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="com.dao.BaseDao">
<select id="queryUserByid" parameterType="int" resultType="com.bean.User">
SELECT * FROM User WHERE ID = #{id}
</select>
<resultMap id="userlist" type="com.bean.User">
<result property="id" column="id"/>
<result property="username" column="username"/>
<result property="password" column="password"/>
<result property="money" column="money"/>
</resultMap>
<select id="selectAll" parameterType="int" resultMap="userlist">
SELECT * FROM User
</select>
</mapper>
编写BaseDAO接口
BaseDao接口:
这里要注明@MapperScan否则Spring容器扫描注入会失败,且方法名要跟UserMapper.xml映射文件里面的查询id名一样,即可不用编写实现类,会自动调用查询语句
@MapperScan
public interface BaseDao {
List<User> selectAll();
}
编写UserAction
UserAction(Controller):.
@Controller
public class UserAction{
@Resource(name = "baseDao")
private BaseDao baseDao;
@RequestMapping(name="/selectall")
public String selectAll() {
List<User> list= baseDao.selectAll();
for (User user : list) {
System.out.println(user.getUsername());
}
return "list";
}
public BaseDao getBaseDao() {
return baseDao;
}
public void setBaseDao(BaseDao baseDao) {
this.baseDao = baseDao;
}
}
编写db.properties
c.driver=com.mysql.jdbc.Driver
c.url=jdbc:mysql:///springc3p0
c.user=root
c.password=root
编写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:p="http://www.springframework.org/schema/p" 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 http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.*"></context:component-scan>
<context:property-placeholder location="classpath:db.properties" />
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${c.driver}"></property>
<property name="jdbcUrl" value="${c.url}"></property>
<property name="user" value="${c.user}"></property>
<property name="password" value="${c.password}"></property>
</bean>
<bean id="sqlsessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="mapperLocations" value="classpath:com/bean/*Mapper"></property>
</bean>
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.bean"></property>
<property name="sqlSessionFactoryBeanName" value="sqlsessionFactory"></property>
</bean>
</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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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-4.3.xsd">
<context:component-scan base-package="com.action"></context:component-scan>
<mvc:annotation-driven></mvc:annotation-driven>
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--设置前缀和后缀-->
<property name="prefix" value=""></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
到此全部编写完成,其中难理解的地方就是为什么方法名和映射文件中id名相同即可调用,在此博主了解的也不是很清晰,随着学习,博主一定会回来透彻的讲解为什么能映射过去