自学笔记--Maven项目的Spring+SpringMVC+MyBatis整合(亲测可用)

  对于一个初学者来说,在整合SSM(Spring+SpringMVC+MyBatis)框架时可能会出现各种问题,博主在经过数次的踩坑之后,总算将三大框架整合到了一起,下面就通过实现对数据库表的查询功能来看看是怎么整合的吧
  

  • 首先我们来看一下在项目中的所需要的配置文件
    mapper.xml—>MyBatis框架映射器映射文件
    spring.xml—>spring框架的配置文件
    springmvc.xml—>springmvc框架的配置文件
    conf.xml—>MyBatis框架的全局配置文件
    db.properties—>属性文件
    pom.xml—>Maven项目的配置文件
    web.xml—>web项目的配置文件

    在Maven项目中,配置文件统一存放于src/main/resources路径下

    >>.mapper.xml中配置的内容
       该文件负责将java对象与数据库表产生映射关系,如果采用动态代理方式的话,将该文件与接口放在一起,具体的配置如下:


<mapper namespace="com.qhit.dao.EmployeeDao">


    <resultMap type="employeepojos" id="pojosresult">
        <id property="id" column="id" />
        <result property="name" column="name" />
        <result property="sex" column="sex" javaType="String" jdbcType="INTEGER" />
        <result property="age" column="age" />
        <result property="address" column="address" />
        <result property="salary" column="salary" />
    resultMap>

    
    <select id="selectAll" resultMap="pojosresult">
        select * from employee
    select>
mapper>

dao层(数据访问层)对应的内容

public interface EmployeeDao {

    // 查询全部数据
    List selectAll();

}

>>.conf.xml中配置的内容
该文件是MyBatis的全局配置文件,具体的配置信息如下:

<configuration>
    
    <typeAliases>
        <package name="com.qhit.pojos" />
    typeAliases>

    
    <typeHandlers>
        <typeHandler handler="com.qhit.converter.SexConverter"
            jdbcType="INTEGER" javaType="String" />
    typeHandlers>

configuration>

>>.pom.xml文件中配置的内容
简单来说.该文件是Maven用来管理项目中jar包的配置文件,具体配置如下:

  jar包配置信息的话就不贴了,小伙伴们可以根据以下jar包名称到maven仓库查找相应的jar包配置信息,如下所示:
自学笔记--Maven项目的Spring+SpringMVC+MyBatis整合(亲测可用)_第1张图片

  重点来看一下在pom.xml文件中的这条配置信息,可以防止配置文件加载不到的情况,具体配置信息如下:

<build>
        <finalName>mybatisfinalName>
        <resources>
            <resource>
                <directory>src/main/javadirectory>
            resource>
            <resource>
                <directory>src/main/resourcesdirectory>
            resource>
        resources>
build>

>>.web.xml文件中配置的内容

    
    <context-param>
        <param-name>contextConfigLocationparam-name>
        <param-value>classpath:spring.xmlparam-value>
    context-param>

    
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    listener>

    
    <servlet>
        <servlet-name>springDispatcherServletservlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
        <init-param>
            <param-name>contextConfigLocationparam-name>
            <param-value>classpath:springmvc.xmlparam-value>
        init-param>
        <load-on-startup>1load-on-startup>
    servlet>

    <servlet-mapping>
        <servlet-name>springDispatcherServletservlet-name>
        <url-pattern>/url-pattern>
    servlet-mapping>

    
    <filter>
        <filter-name>characterEncodingFilterfilter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
        <init-param>
            <param-name>encodingparam-name>
            <param-value>UTF-8param-value>
        init-param>
    filter>
    <filter-mapping>
        <filter-name>characterEncodingFilterfilter-name>
        <servlet-name>springDispatcherServletservlet-name>
    filter-mapping>

>>.db.properties文件中配置的内容

driver=oracle.jdbc.driver.OracleDriver
url=jdbc:oracle:thin:@localhost:1521:orcl
name=数据库登录名
pass=数据库登录密码

>>.spring.xml文件中配置的内容


    <context:property-placeholder location="classpath:db.properties" />

    
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
        p:driverClass="${driver}" p:jdbcUrl="${url}" p:user="${name}"
        p:password="${pass}" p:maxIdleTime="1000" p:maxPoolSize="30"
        p:minPoolSize="5" p:autoCommitOnClose="false">bean>

    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"
        p:dataSource-ref="dataSource" p:mapperLocations="classpath:com/qhit/dao/employeeMapper.xml"
        p:configLocation="classpath:conf.xml">bean>

    
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"
        p:basePackage="com.qhit.dao" p:sqlSessionFactoryBeanName="sqlSessionFactory">bean>

    
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
        p:dataSource-ref="dataSource">bean>

    
    <tx:annotation-driven transaction-manager="transactionManager" />

    
    <context:component-scan base-package="com.qhit.service.impl,com.qhit.controller" />

>>.springmvc.xml文件中配置的内容

 
    <mvc:annotation-driven />

    
    <mvc:default-servlet-handler />

    
    <context:component-scan base-package="com.qhit.service.impl,com.qhit.controller" />

    
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver" />

service层(服务层)测试代码

@Service
public class EmployeeServiceImpl implements EmployeeService {

    @Autowired
    private EmployeeDao dao;

    /*
     * @author Virus
     * 
     * @description 查询全部员工信息
     */
    @Override
    public List selectAll() {
        // TODO Auto-generated method stub
        return this.dao.selectAll();
    }
}

controller层(控制层)测试代码

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeServiceImpl service;
    private ModelAndView ms;

    // 获取全部信息
    @RequestMapping("/all")
    public ModelAndView allInfo() {
        System.out.println("进来了......");
        ms = new ModelAndView();
        ms.addObject("allEmployee", this.service.selectAll());
        ms.setViewName("forward:/index.jsp");
        return ms;
    }
}

JSP页面测试代码

  • getinfo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
head>
<body>
<jsp:forward page="employee/all" />
body>
html>
  • index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title heretitle>
<style type="text/css">
th {
    text-align: center;
}
style>
head>
<body>
    <table border="1">
        <tr>
            <th>编号th>
            <th>姓名th>
            <th>性别th>
            <th>年龄th>
            <th>家庭住址th>
            <th>薪资th>
        tr>
        <c:forEach items="${allEmployee}" var="all">
            <tr>
                <th>${all.id }th>
                <th>${all.name }th>
                <th>${all.sex }th>
                <th>${all.age }th>
                <th>${all.address }th>
                <th>${all.salary }th>
            tr>
        c:forEach>
    table>
body>
html>
  • 查询结果
    自学笔记--Maven项目的Spring+SpringMVC+MyBatis整合(亲测可用)_第2张图片

Ending……

本文还存在有很多不足之处,希望小伙伴们可以多多指教 ^_^

革命尚未成功,同志仍需努力啊!!!

你可能感兴趣的:(Maven,SSM整合)