• 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏吧

SpringMVC第三天

spring-mvc 来源:浅川好孩子 33次浏览

mybatis
只要写接口就行了,帮你生成实现类
复制Controller的注解不被扫描


引入的test的scope改变范围

创建的maven项目,右键new ->XML Configuration File 时,无Spring Config选项



idea新建Maven项目后刷新依赖的两种方式

01整合说明

原理
用spring整合另外两个框架

02搭建环境

导入坐标
<?xml version=”1.0″ encoding=”UTF-8″?>

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/m&#8230;d”>
<modelVersion>4.0.0</modelVersion>

<groupId>cn.itcast</groupId>
<artifactId>ssm</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>ssm Maven Webapp</name>
<!– FIXME change it to the project’s website –>
<url>http://www.example.com</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<spring.version>5.0.2.RELEASE</spring.version>
<slf4j.version>1.6.6</slf4j.version>
<log4j.version>1.2.12</log4j.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.version>3.4.5</mybatis.version>

</properties>

<dependencies>

<!-- spring -->
<dependency>
  <groupId>org.aspectj</groupId>
  <artifactId>aspectjweaver</artifactId>
  <version>1.6.8</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-web</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-webmvc</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-test</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>${spring.version}</version>
</dependency>

<dependency>
  <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>4.12</version>
  <scope>compile</scope>
</dependency>

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>${mysql.version}</version>
</dependency>

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>javax.servlet.jsp</groupId>
  <artifactId>jsp-api</artifactId>
  <version>2.0</version>
  <scope>provided</scope>
</dependency>

<dependency>
  <groupId>jstl</groupId>
  <artifactId>jstl</artifactId>
  <version>1.2</version>
</dependency>

<!-- log start -->
<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>${log4j.version}</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>${slf4j.version}</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>${slf4j.version}</version>
</dependency>

<!-- log end -->
<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis</artifactId>
  <version>${mybatis.version}</version>
</dependency>

<dependency>
  <groupId>org.mybatis</groupId>
  <artifactId>mybatis-spring</artifactId>
  <version>1.3.0</version>
</dependency>

<dependency>
  <groupId>c3p0</groupId>
  <artifactId>c3p0</artifactId>
  <version>0.9.1.2</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>

</dependencies>

<build>

<finalName>ssm</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  <plugins>
    <plugin>
      <artifactId>maven-clean-plugin</artifactId>
      <version>3.0.0</version>
    </plugin>
    <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
    <plugin>
      <artifactId>maven-resources-plugin</artifactId>
      <version>3.0.2</version>
    </plugin>
    <plugin>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.7.0</version>
    </plugin>
    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>2.20.1</version>
    </plugin>
    <plugin>
      <artifactId>maven-war-plugin</artifactId>
      <version>3.2.0</version>
    </plugin>
    <plugin>
      <artifactId>maven-install-plugin</artifactId>
      <version>2.5.2</version>
    </plugin>
    <plugin>
      <artifactId>maven-deploy-plugin</artifactId>
      <version>2.8.2</version>
    </plugin>
  </plugins>
</pluginManagement>

</build>
</project>

基本结构

03项目整合spring框架

在resources下创建applicationContext.xml

<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans&#8221;

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   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.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">

<!--开启注解的扫描,希望处理service和dao,controller不需要Spring框架去处理-->
<context:component-scan base-package="cn.itcast" >
    <!--配置哪些注解不扫描-->
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

</beans>

service实现类添加注解

test目录下进行单元测试

04项目整合springmvc框架

配置web.xml
<!DOCTYPE web-app PUBLIC
“-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
http://java.sun.com/dtd/web-app_2_3.dtd&#8221; >

<web-app>
<display-name>Archetype Created Web Application</display-name>

<!–配置前端控制器–>
<servlet>

<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc.xml配置文件-->
<init-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:springmvc.xml</param-value>
</init-param>
<!--启动服务器,创建该servlet-->
<load-on-startup>1</load-on-startup>

</servlet>
<servlet-mapping>

<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</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>

</filter>
<filter-mapping>

<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>

</filter-mapping>

</web-app>
配置springmvc.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
<beans xmlns=”http://www.springframework.org/schema/beans&#8221;

   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">

<!--开启注解扫描,只扫描Controller注解-->
<context:component-scan base-package="cn.itcast">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!--配置的视图解析器对象-->
<bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/pages/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<!--过滤静态资源-->
<mvc:resources location="/css/" mapping="/css/**" />
<mvc:resources location="/images/" mapping="/images/**" />
<mvc:resources location="/js/" mapping="/js/**" />

<!--开启SpringMVC注解的支持-->
<mvc:annotation-driven/>

</beans>
测试

05spring整合spring mvc框架

要配置监听器

其中spring web可以提供一个类

复制这个类的路径
org.springframework.web.context.ContextLoaderListener

这个类有
contextInitialized
contextDestroyed
启动和销毁这两个方法
在web.xml中配置监听器
<!–配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件–>
<listener>

<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

</listener>
<!–设置配置文件的路径–>
<context-param>

<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>

</context-param>

06整合Mybatis框架

accountDao中加注解

resources下添加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>

<environments default="mysql">
    <environment id="mysql">
        <transactionManager type="JDBC"></transactionManager>
        <dataSource type="POOLED">
            <property name="driver" value="com.mysql.jdbc.Driver"/>
            <property name="url" value="jdbc:mysql:///ssm?characterEncoding=utf8"/>
            <property name="username" value="root"/>
            <property name="password" value=""/>
        </dataSource>
    </environment>
</environments>
<!--引入配置文件 扫描cn.itcast.dao下的所有接口-->
<mappers>
    <package name="cn.itcast.dao"/>
</mappers>

</configuration>

07编写Mybatis的测试方法

package cn.itcast.test;

import cn.itcast.dao.AccountDao;
import cn.itcast.domain.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.InputStream;
import java.util.List;

public class TestMyBatis {

/**
 * 测试查询
 * @throws Exception
 */
@Test
public void run1() throws Exception {
    // 加载配置文件
    InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
    // 创建SqlSessionFactory对象
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
    // 创建SqlSession对象
    SqlSession session = factory.openSession();
    // 获取到代理对象
    AccountDao dao = session.getMapper(AccountDao.class);
    // 查询所有数据
    List<Account> list = dao.findAll();
    for(Account account : list){
        System.out.println(account);
    }
    // 关闭资源
    session.close();
    in.close();
}

/**
 * 测试保存
 * @throws Exception
 */
@Test
public void run2() throws Exception {
    Account account = new Account();
    account.setName("熊大");
    account.setMoney(400d);

    // 加载配置文件
    InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
    // 创建SqlSessionFactory对象
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
    // 创建SqlSession对象
    SqlSession session = factory.openSession();
    // 获取到代理对象
    AccountDao dao = session.getMapper(AccountDao.class);

    // 保存
    dao.saveAccount(account);

    // 提交事务
    session.commit();

    // 关闭资源
    session.close();
    in.close();
}

}

08spring整合mybatis框架

配置

dao层

controller层

09配置事务管理器

spring的配置文件

execution()的参数是
public cn.itcast.service.ServiceImpl.*(..) 其中public可以省略


版权声明:本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系管理员进行删除。
喜欢 (0)