zoukankan      html  css  js  c++  java
  • spring入门(六) spring mvc+mybatis

    1.引入依赖

     1 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
     2 <dependency>
     3     <groupId>org.mybatis</groupId>
     4     <artifactId>mybatis</artifactId>
     5     <version>3.4.5</version>
     6 </dependency>
     7 
     8 <!--spring的版本是5.0.9,mybatis-spring的版本要是1.3+ -->
     9 <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
    10 <dependency>
    11     <groupId>org.mybatis</groupId>
    12     <artifactId>mybatis-spring</artifactId>
    13     <version>1.3.2</version>
    14 </dependency>

    2.在springmvc-config.xml增加mybatis配置

     1 <!-- 针对myBatis的配置项 -->
     2 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
     3     <!-- 实例化sqlSessionFactory时需要使用上述配置好的数据源以及SQL映射文件 -->
     4     <property name="dataSource" ref="dataSource" />
     5     <!-- 自动扫描classpath:mapper/*.xml         -->
     6     <property name="mapperLocations" value="classpath:mapper/*.xml" />
     7 </bean>
     8 <!-- 配置扫描器 -->
     9 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    10     <!-- 扫描com.ice.dao这个包以及它的子包下的所有映射接口类,省去了在每个mapper接口上注解 -->
    11     <property name="basePackage" value="com.ice.dao" />
    12     <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    13 </bean>

    3.在classpath:mapper/ 创建CustomerMapper.xml

    注意创建的位置,是在上面设置的 mapperLocations.

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     3 <!--  namespace要跟后面该xml对应的mapper类的全名一致 -->
     4 <mapper namespace="com.ice.dao.CustomerMapper">
     5     <!--  id设置为mapper类对应的方法名, parameterType跟方法的参数类型一致,resultType跟方法的返回类型一致.
     6     如果返回单个对象,mybatis会自动调用selectOne方法;如果返回的是List,mybatis会自动调用selectList方法.
     7     -->    
     8     <select id="getUser" parameterType="java.lang.String"
     9             resultType="com.ice.model.Customer">
    10         select id,name from customer where name=#{name}
    11     </select>
    12 </mapper>

    4.创建CustomerMapper.xml对应的CustomerMapper.java

    注意创建的位置,是在上面设置的 basePackage.

    1 package com.ice.dao;
    2 
    3 import com.ice.model.Customer;
    4 //不用在xml里配置该bean,也不用注解.已经配置了MapperScanner 
    5 //<property name="basePackage" value="com.ice.dao" />
    6 public interface CustomerMapper {
    7 
    8     Customer getUser(String name);
    9 }

    5.简单测试(在controller里测试,也可考虑springTest)

    正式项目里,要有service层.

     1 package com.ice.controller;
     2 
     3 import com.ice.dao.CustomerMapper;
     4 import com.ice.model.Customer;
     5 import org.springframework.beans.factory.annotation.Autowired;
     6 import org.springframework.stereotype.Controller;
     7 import org.springframework.web.bind.annotation.RequestMapping;
     8 import org.springframework.web.bind.annotation.ResponseBody;
     9 
    10 @Controller
    11 @RequestMapping("/mybatis")
    12 public class MybatisController {
    13 
    14     @Autowired
    15     private CustomerMapper customerMapper;
    16 
    17     @RequestMapping("/query")
    18     @ResponseBody
    19     public String index() {
    20         Customer user = customerMapper.getUser("hello");
    21         return "顾客id" + user.getId();
    22     }
    23 }
  • 相关阅读:
    English Dictionary site for ODE and OALD
    vmic environment
    makefile
    the diference between include and import
    windows 工具命令 cmd
    python namespace
    shell cmd args
    ROE, ROC
    IP
    链接及常用软件
  • 原文地址:https://www.cnblogs.com/ICE_Inspire/p/9744593.html
Copyright © 2011-2022 走看看