spring入门(六) spring mvc+mybatis
2021-05-18 18:28
标签:end nbsp doc sele https session pac 1.3 map 注意创建的位置,是在上面设置的 mapperLocations. 注意创建的位置,是在上面设置的 basePackage. 正式项目里,要有service层. spring入门(六) spring mvc+mybatis 标签:end nbsp doc sele https session pac 1.3 map 原文地址:https://www.cnblogs.com/ICE_Inspire/p/9744593.html1.引入依赖
1
2 dependency>
3 groupId>org.mybatisgroupId>
4 artifactId>mybatisartifactId>
5 version>3.4.5version>
6 dependency>
7
8
9
10 dependency>
11 groupId>org.mybatisgroupId>
12 artifactId>mybatis-springartifactId>
13 version>1.3.2version>
14 dependency>
2.在springmvc-config.xml增加mybatis配置
1
2 bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
3
4 property name="dataSource" ref="dataSource" />
5
6 property name="mapperLocations" value="classpath:mapper/*.xml" />
7 bean>
8
9 bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
10
11 property name="basePackage" value="com.ice.dao" />
12 property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
13 bean>
3.在classpath:mapper/ 创建CustomerMapper.xml
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
4 mapper namespace="com.ice.dao.CustomerMapper">
5
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
1 package com.ice.dao;
2
3 import com.ice.model.Customer;
4 //不用在xml里配置该bean,也不用注解.已经配置了MapperScanner
5 //
5.简单测试(在controller里测试,也可考虑springTest)
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 }
文章标题:spring入门(六) spring mvc+mybatis
文章链接:http://soscw.com/index.php/essay/87302.html