SSM(Spring,SpringMVC,Mybatis)项目整合配置与应用

时间:2021-01-10 08:52:34

项目视图:
SSM(Spring,SpringMVC,Mybatis)项目整合配置与应用
1.配置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>Sport</display-name>
<!-- 加载spring容器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/applicationContext-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- springmvc前端控制器 -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- contextConfigLocation配置springmvc加载的配置文件 -->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/springmvc.xml</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.action</url-pattern>
</servlet-mapping>
</web-app>

2.
(1)配置springmvc.xml

<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"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "
>


<!-- 扫描controller注解,多个包中间使用半角逗号分隔 -->
<context:component-scan base-package="com.sport.controller" />
<!-- 注解驱动器,配置后,可以扫描注解 -->
<mvc:annotation-driven />
<!-- ViewResolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView" />

<property name="prefix" value="/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>

(2)配置mybatis所需的文件:
1.db.properties:

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/数据库名
jdbc.username=用户名
jdbc.password=密码

2.配置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>
<!---->
<settings>
<setting name="lazyLoadingEnabled" value="false" />
</settings>
<!--自定义别名-->
<typeAliases>
<package name="com.sport.po" />
</typeAliases>
</configuration>

3.在Spring容器中整合Mybatis:
配置所需的数据源,数据连接池,mapper扫描器

<?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"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "
>


<!--加载mybatis所需的db.properties-->
<context:property-placeholder location="classpath:mybatis/db.properties" />
<!-- 配置数据源 ,dbcp -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">

<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
<property name="maxActive" value="30" />
<property name="maxIdle" value="5" />
</bean>
<!-- sqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 数据库连接池 -->
<property name="dataSource" ref="dataSource" />
<!-- 加载mybatis的全局配置文件 -->
<property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml" />
</bean>
<bean id="sqlTemplate" class="org.mybatis.spring.SqlSessionTemplate">
<constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" />
</bean>
<!-- mapper扫描器 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.sport.mapper"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>
</beans>

4.Spring中配置Service层:

<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"
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-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "
>

<!-- 会自动扫描注解 -->
<context:component-scan base-package="com.sport.service" />
</beans>

登录测试Demo:
SSM(Spring,SpringMVC,Mybatis)项目整合配置与应用

登录成功跳转页面:

SSM(Spring,SpringMVC,Mybatis)项目整合配置与应用

1:mapper层
通过用户名和密码,查询数据库中是否存在。

public interface AccountMapper {
//用户名和管理员登录
public User userLogin(User user);
public Manager managerLogin(Manager manager);
}

注意:
mapper.xml的namespace就是mapper接口的完成路径。mapper.xml中的id和mapper接口的方法名相对应,接受参数和返回参数的类型,都一一对应。

<mapper namespace="com.sport.mapper.AccountMapper">
<select id="userLogin" resultType="com.sport.po.User" parameterType="com.sport.po.User">
select * from user where username=#{username} and password=#{password}
</select>

<select id="managerLogin" resultType="com.sport.po.Manager" parameterType="com.sport.po.Manager">
select * from manager where loginname=#{loginname} and password=#{password}
</select>

2.Service层:


@Service
//@Service标识这是一个service层,相当于配置文件中的<context:component-scan base-package="com.sport.service" />
public class AccountService {
@Autowired//自动将这个mapper接口注入
private AccountMapper accountMapper;

public User userLogin(User user){
return accountMapper.userLogin(user);
}

public Manager managerLogin(Manager manager){
return accountMapper.managerLogin(manager);
}
}

如果这里将注解去掉 如:

public class AccountService {

private AccountMapper accountMapper;

public User userLogin(User user){
return accountMapper.userLogin(user);
}

public Manager managerLogin(Manager manager){
return accountMapper.managerLogin(manager);
}
}

在配置文件中,将
<context:component-scan base-package="com.sport.service" />替换成

<bean id="accountService" class="com.sport.service.AccountService">
<property name="accountMapper" ref="accountMapper"></property>
</bean>

3.Controller层:


@Controller
public class AccountAction {

@Autowired
private AccountService accountService;
//登录
//表示通过LoginAction.action能访问到这个方法
@RequestMapping("/LoginAction.action")
//从jsp接受的参数,注意这里和jsp标签的name应该一一对应。
public String login(String login_name,String password,String role,HttpSession session){
if(role.equalsIgnoreCase("user")){
User user=new User();
user.setPassword(password);
user.setUsername(login_name);
System.out.println(user);
user=accountService.userLogin(user);
System.out.println("login:"+user);
if(user!=null){
session.setAttribute("user", user);
return "User1";
//返回的值表示所要返回的页面,这里指登录成功后的跳转
}
}else if(role.equalsIgnoreCase("manager")){
Manager manager=new Manager();
manager.setLoginname(login_name);
manager.setPassword(password);
System.out.println(manager);
manager=accountService.managerLogin(manager);
System.out.println("login:"+manager);
if(manager!=null){
session.setAttribute("manager", manager);
return "manager";
}
}
return null;
}
}