• ssm整合


    **************************

     配置mabatis接口代理开发

    *接口名和映射文件必须同名
    *在同一个包下
    *输入值,返回值都必须对应
    *映射文件namespace必须和接口的全路径类名想同

    **************************************************************************************************************************************************

    1.创建项目  命名

    2.导入jar

    3.进入web.xml 进行相关配置

      1)字符集的配置

      <!-- 用过滤器  设置编码类型为utf-8 -->

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

         2)加载spring配置文件

    <!--加载spring配置文件-->

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

    3)加载springmvc配置文件

    <servlet>

       <servlet-name>springmvc</servlet-name>

       <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

        

        <!-- 设置springmvc配置文件位置 -->

        <init-param>

          <param-name>contextConfigLocation</param-name>

          <param-value>classpath:springmvc.xml</param-value>

          

        </init-param>

    </servlet>

    <servlet-mapping>

       <servlet-name>springmvc</servlet-name>

        <url-pattern>*.do</url-pattern>

    </servlet-mapping>

    4.创建applicationcontext.xml文件

      

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

    <!-- 扫描器 -->
    <context:component-scan base-package="cn.itcast"/>


    <!-- 配置连接池: -->
    <!-- 引入外部属性文件 -->
    <!-- 配置C3P0连接池: -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    <property name="driverClass" value="com.mysql.jdbc.Driver"/>
    <property name="jdbcUrl" value="jdbc:mysql:///shop"/>
    <property name="user" value="root"/>
    <property name="password" value="1234"/>
    </bean>

    <!-- 创建sqlsessionfactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <!-- 指定mybatis的全局配置文件的路径 -->
    <property name="configLocation" value="classpath:sqlmapconfig.xml"></property>
    <!-- 数据源 -->
    <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 配置mabatis接口代理开发
    *接口名和映射文件必须同名
    *在同一个包下
    *输入值,返回值都必须对应
    *映射文件namespace必须和接口的全路径类名想同


    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

    <property name="basePackage" value="cn.itcast.dao"></property>
    <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>
    <!-- 事务管理: -->
    <!-- 事务管理器 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
    </bean>
    <!-- 开启注解事务 -->
    <tx:annotation-driven transaction-manager="transactionManager"/>
    </beans>

    5创建springmvc.xml文件

      

    <?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">
    <!-- 扫描器:扫描该包或者该包子包下的所有带有某些注解的类,建立一个bean -->
    <context:component-scan base-package="cn.itcast"/>

    <!-- 默认创建注解处理器映射器和处理器适配器,还提供了json数据格式的支持 -->
    <mvc:annotation-driven/>
    <!-- 配置视图解析器 -->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/jsps/"></property>
    <property name="suffix" value=".jsp"></property>
    </bean>
    </beans>

    6 粘贴jdbc.property文件(可有可无)

    7创建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>


    </configuration>

    8.创建*mapper.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.itcast.dao.UserMapper">
    <select id="find" resultType="cn.itcast.domain.User">

    select * from adminuser
    </select>

    </mapper>

    9.创建*mapper.java文件

    package cn.itcast.dao;

    import java.util.List;

    import cn.itcast.domain.User;

    public interface UserMapper {
    public List<User> find();

    }

    10 创建控制类

    package cn.itcast.controller;

    import java.util.List;

    import javax.annotation.Resource;

    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;

    import cn.itcast.domain.User;
    import cn.itcast.service.UserService;


    @Controller
    @RequestMapping("/user")
    public class UserController {

    @Resource
    private UserService userService;

    @RequestMapping("/showUser")
    public void toIndex(){
    List<User> users = this.userService.find();
    System.out.println(users);
    }
    }

     11创建service

    package cn.itcast.service;

    import java.util.List;

    import javax.annotation.Resource;

    import org.springframework.stereotype.Service;

    import cn.itcast.dao.UserMapper;
    import cn.itcast.domain.User;
    @Service
    public class UserService {

    @Resource
    UserMapper userdao;

    public List<User> find(){
    return userdao.find();
    }

    }

     

  • 相关阅读:
    TCP报文发送工具
    Java基础—注解的使用
    STM32以太网ETH
    EC20 minipcie版4g模块开发笔记
    usb端点(endpoint)知识详解
    STM32 usb_mem.c和usb_sil.c文件的分析
    USB的中断说明
    STM32 可编程电压监测器(PVD)实现数据掉电保存
    关于FSMC地址线的理解
    STM32F4—fsmc的配置步骤
  • 原文地址:https://www.cnblogs.com/fdbk/p/8918657.html
Copyright © 2020-2023  润新知