SSM整合XML版(Maven Project)

一、使用IntelliJ IDEA 创建Maven管理的JAVA Web项目

①新建项目,选择Maven Module

 

 在本地磁盘用户的。.m2目录下生成settings文件:

 

Maven工程搭建完毕!!!


 


 


 ② 开发步骤:

自动从本地仓库下载Jar包:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>cn.wj</groupId>
  <artifactId>SpringMVC_SSM</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>SpringMVC_SSM Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <!--junit配置-->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>

<!--javaee  支持--> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency>
<!--spring+Spring mvc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.3.release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.9.RELEASE</version> </dependency>
 <!--MyBatisjar包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency>
<!--Mybatis整合Spring jar包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.17.1-GA</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency>
<!--驱动jar包--> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.1.0</version> </dependency> </dependencies> <build> <finalName>SpringMVC_SSM</finalName> </build> </project>

 

③ 由于Maven对oracle数据库的不支持,我们需要将Oracle装载到本地仓库,

让Maven使用Oracle

1、配置Maven3.3.9 bin到环境变量

然后可以通过cmd命令行 mvn -version测试有没有测试成功!!!

2、定位到Oracle安装目录的jdbc文件夹

 


④搭建分层架构---实现添加学生


实体类UserInfo:

package cn.wj.entity;

/**
 * Created by WJ on 2017/1/16.
 */
public class UserInfo {

    private Integer id;
    private String name;
    private Integer age;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }
}

 dao接口: 无需dao实现类,通过代理生成

public interface IUserInfoDao {
    public void add(UserInfo info);

}

 service层:

public interface IUserInfoService {

    public void add(UserInfo info);
}

 service实现类:

public class UserInfoServiceImpl implements IUserInfoService {
    private IUserInfoDao dao;
    public void add(UserInfo info) {
        dao.add(info);
    }

    public IUserInfoDao getDao() {
        return dao;
    }

    public void setDao(IUserInfoDao dao) {
        this.dao = dao;
    }
}

 控制器类:controller 

public class UserInfoController implements Controller {

    private IUserInfoService service;

    public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {

        String uname=httpServletRequest.getParameter("uname");
        Integer uage=Integer.valueOf(httpServletRequest.getParameter("uage"));

        UserInfo info=new UserInfo();
        info.setName(uname);
        info.setAge(uage);

        service.add(info);

        return new ModelAndView("/welcome.jsp");
    }

    public IUserInfoService getService() {
        return service;
    }

    public void setService(IUserInfoService service) {
        this.service = service;
    }
}

 由于是整合MyBatis,需要书写核心配置文件:

小配置:UserInfoDao.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="cn.wj.dao.IUserInfoDao">
<select id="add" parameterType="UserInfo">
insert into userinfo values(SEQ_Num.nextval,#{name},#{age})
</select>

</mapper>

 MyBatis配置文件:mybatis-config.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>
<typeAliases>
<package name="cn.wj.entity"/>
</typeAliases>


<mappers>

   <package name="cn.wj.dao"/>

</mappers>
</configuration>

 applicationContext.xml:进行植入dao、service、controller、事务管理器、数据源、sessionFactory 

 


index.jsp页面:

<body>
<form action="${pageContext.request.contextPath }/userAction.do" method="post">
    用户名:<input name="uname"/> <br/><br/>
    年龄:<input name="uage"/><br/><br/>
    <input type="submit" value="add"/>
</form>
</body>

 Web.xml:进行配置中央调度器 

<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_3_1.xsd"
         version="3.1">
  <!-- 1.针对Spring配置:读取配置文件 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <!-- 注册ServletContext监听器,创建容器对象,并且将ApplicationContext对象放到Application域中 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 针对SpringMVC的配置::::::中央调度器:本质上一个serlvet            配置的关于SpringmVC组件 -->
  <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>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- 解决乱码的过滤器 -->
  <filter>
    <filter-name>CharacterEncodingFilter</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>

    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>

  <filter-mapping>
    <filter-name>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

  实现效果:

 


 注:若出现此类异常--org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

解决方案:参见博客

http://www.cnblogs.com/jstarseven/p/5803697.html

http://blog.csdn.net/shifangwannian/article/details/48882201 
posted @ 2017-01-17 10:01  Fighting`  阅读(1349)  评论(1编辑  收藏  举报