一.使用idea简单开发springmvc+mybaties

主要步骤:

一:使用idea创建maven项目

二:将maven项目转换为web项目

三:在web项目添加依赖的包

四:整合tomcat进行测试,先看能不能正常访问部署

五:在web.xml中整合spring mvc,接收请求,返回json对象

六.进行测试

七.整合spring mvc和mybaties,访问数据库

 7.1.创建表和对象映射的pojo实体类

 7.2.使用接口与xml的形式完成对象与关系间的映射

 7.3.在web.xml中整合spring mvc

 7.4.添加业务层和控制层

 八.进行测试

第一:创建简单的maven项目

1.1.创建一个新的项目



1.2.填写好包名、项目名.如下图所示:

1.3.创建好的maven项目结构如下图所示:


二:将maven项目转换为web项目
2.1.将项目转换成web项目.如下图所示

选择项目--->右键---->Add Framework Support--->选择java EE下面的Web Application,ps:请记得选择创建web.xml

转换成功之后:

三:在web项目添加依赖的包

3.1修改maven的pom.xml文件创建好后添加依赖包

<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.ivy</groupId>
    <artifactId>springmvcDemo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <repositories><!-- 代码库 -->
        <repository>
            <id>maven-ali</id>
            <url>http://maven.aliyun.com/nexus/content/groups/public//</url>
            <releases>
                <enabled>true</enabled>
            </releases>
            <snapshots>
                <enabled>true</enabled>
                <updatePolicy>always</updatePolicy>
                <checksumPolicy>fail</checksumPolicy>
            </snapshots>
        </repository>
    </repositories>
      



    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <spring.version>4.3.0.RELEASE</spring.version>
    </properties>

    <dependencies>
        <!--Spring框架核心库 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- Spring MVC -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!-- aspectJ AOP 织入器 -->
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
            <version>1.8.9</version>
        </dependency>
        <!--mybatis-spring适配器 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--Spring java数据库访问包,在本例中主要用于提供数据源 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <!--mysql数据库驱动 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.38</version>
        </dependency>

        <!--log4j日志包 -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.6.1</version>
        </dependency>
        <!-- mybatis ORM框架 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.1</version>
        </dependency>
        <!-- JUnit单元测试工具 -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
        </dependency>
        <!--c3p0 连接池 -->
        <dependency>
            <groupId>c3p0</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.1.2</version>
        </dependency>
        <!-- JSTL -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <!-- Servlet核心包 -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.0.1</version>
            <scope>provided</scope>
        </dependency>
        <!--JSP -->
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>jsp-api</artifactId>
            <version>2.1</version>
            <scope>provided</scope>
        </dependency>
        <!--Json-->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.9.2</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>maven-springmvc</finalName>
        <resources>
            <!--表示把java目录下的有关xml文件,properties文件编译/打包的时候放在resource目录下-->
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <plugins>

        </plugins>
    </build>

</project>

四:整合tomcat进行测试,先看能不能正常访问部署

4.1选择项目右上角的“Edit Configurations”


4.2添加tomcat

选择tomcat server  --->选择local,


填写名字,添加部署包,在server的选项卡中的

OnUpdateAction选择UpdateClassesAndResources
OnFrameDeactivation选择UpdateClassesAndResources

4.3设置参数

在deployment的选项卡中---->点击“+”,指定一个虚拟目录的名字 在Application Context:/访问的目录名,设置应用上下文,访问的路径


4.4.将依赖包添加到Web-Inf目录下,要不然会找不到这些类,千万要记住,一定要做

运行tomcat,看能不能正常访问



五:在web.xml中整合spring mvc,接收请求,返回json对象


5.1在web.xml添加springmvc的中心控制器DispatchServlet用于接收请求

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <!-- 第一步:设置Servlet访问的路径映射,所有的访问都必须经过调度用的前置控制品 -->
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!--第二步:添加Spring MVC 前置Servlet,中心控制器 ,当有请求访问的时候会交给中心控制器DispatcherServlet进行处理,初始化spring/spring-mvc.xml-->
    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <!--Spring MVC配置文件路径 -->
            <param-value>classpath*:spring/spring-mvc.xml</param-value>
        </init-param>
        <!-- 启动动优先级,越小越早加载 -->
        <load-on-startup>1</load-on-startup>

    </servlet>


    <!--第三步:为了防止乱码,添加编码过滤器 -->
    <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>

5.2在src下面的resouce添加spring/spring-mvc配置文件,这个文件很重要,在这个文件中通过配置

<context:component-scan base-package="com.ivy" />
 自动扫描指定的包,使SpringMVC认为指定包下的类用了某些注解具有不同的含义,eg:用了
@controller注解的类是控制器,

<?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:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd">
    <context:component-scan base-package="com.ivy" />
    </beans>


5.3在src/main/java下面新建控制层,添加相应的测试类

package com.ivy.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/4/16.
 */
@Controller  //声明为控制器
@RequestMapping(path = "/a")  //请求映射
public class Test {

    @RequestMapping(path = "/json")  //请求映射
    public ModelAndView handle(){
        ModelAndView mav=new ModelAndView(new MappingJackson2JsonView());
        List<Long> testList=new ArrayList();
        testList.add(1L);
        testList.add(2L);
        testList.add(3L);
        mav.addObject(testList);
        return mav;

    }
}

六.重新启动tomcat进行测试


七.整合spring mvc和mybaties,访问数据库


 7.1.在web.xml中,添加ContextLoaderListener监听器初始化上下文配置文件

  <listener>
        <description>Spring容器加载监听器</description>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <description>设置Spring加载时的配置文件位置,默认位置在WEB-INF/lib目录下</description>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring/applicationContext.xml</param-value>
    </context-param>

 7.2.在src下面的resouce添加aplicationContext配置文件,用于初始化数据源和会话工厂

<?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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.3.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
    <!--第一步:配置C3P0数据源 -->
    <bean name="datasource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="user" value="root"></property>
        <property name="password" value="123456"></property>
    </bean>

    <!--第二步:配置会话工厂bean sqlSessionFactoryBean -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 数据源 -->
        <property name="dataSource" ref="datasource"/>
        <!-- sql映射文件路径 -->
        <property name="mapperLocations" value="classpath*:mapping/*Mapper.xml"></property>
    </bean>

    <!--第三步:自动扫描对象关系映射 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!--指定会话工厂,如果当前上下文中只定义了一个则该属性可省去 -->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        <!-- 指定要自动扫描接口的基础包,实现接口 -->
        <property name="basePackage" value="com.ivy.dao"></property>
    </bean>
</beans>

      在src下面的resouce添加Mapping/实体类Mapper.xml配置文件,在文件中定义sql的映射

<?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.ivy.dao.IUserDao" >
    <select id="findUserName" resultType="com.ivy.model.User">
        select  id,name
        from test
    </select>
</mapper>

 7.3.创建表和对象映射的pojo实体类

package com.ivy.model;

/**
 * Created by Administrator .
 */
public class User {
    private int id;

    public int getId() {
        return id;
    }

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

    private String name;

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "id is"+this.getId()+";name is"+this.getName();
    }
}

 7.4.定义dao接口,使用接口与xml的形式完成对象与关系间的映射

package com.ivy.dao;

import com.ivy.model.User;

import java.util.List;

/**
 * Created by Administrator
 */
public interface IUserDao {


    List<User> findUserName();
}

 7.5.添加业务层和控制层

先定义业务层接口

package com.ivy.service;

import com.ivy.model.User;

import java.util.List;

public interface IUserService { public List<User> findUserName();
}

在定义业务层的实现

package com.ivy.service;

import com.ivy.dao.IUserDao;
import com.ivy.model.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by Administrator 
 */
@Service("userService")
public class UserServiceImpl implements IUserService {
    @Autowired
    IUserDao iUserDao;

    public List<User> findUserName() {
        List<User> a=iUserDao.findUserName();
        System.out.print(a.toString());
        return a;
    }
}

最后在控制层调用

package com.ivy.controller;

import com.ivy.service.IUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJackson2JsonView;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator 
 */
@Controller  //声明为控制器
@RequestMapping(path = "/a")  //请求映射
public class Test {
    @Resource
    IUserService userService;


    @RequestMapping(path = "/daojson")  //请求映射
    public ModelAndView handle2(){
        ModelAndView mav=new ModelAndView(new MappingJackson2JsonView());
        mav.addObject(userService.findUserName());
        return mav;

    }
}

 八.进行测试



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值