以SpringMVC框架为中心疯狂扩展-04、添加mybatis依赖

1、在pom中加入一堆的依赖

<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/maven-v4_0_0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<groupId>com.example</groupId>
	<artifactId>p2.customer</artifactId>
	<packaging>war</packaging>
	<version>0.0.1-SNAPSHOT</version>
	<name>p2.customer Maven Webapp</name>
	<url>http://maven.apache.org</url>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<junit.version>4.12</junit.version>
		<springframework.version>4.3.1.RELEASE</springframework.version>
		<mybatis.version>3.4.1</mybatis.version>
		<mysql.version>6.0.3</mysql.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>${junit.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${springframework.version}</version>
		</dependency>
		<!-- mybatis依赖 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.0</version>
		</dependency>
		<!-- mysql驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>${mysql.version}</version>
		</dependency>
		<!-- 数据库连接池 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-dbcp2</artifactId>
			<version>2.1.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-pool2</artifactId>
			<version>2.4.2</version>
		</dependency>
	</dependencies>
	<build>
		<finalName>p2.customer</finalName>
		<plugins>
			<plugin>
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<version>2.2</version>
				<configuration>
					<port>9090</port>
					<path>/</path>
					<uriEncoding>UTF-8</uriEncoding>
					<finalName>p2</finalName>
					<server>tomcat7</server>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

等待building完成

2、新建一个spring-mybatis.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: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.1.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
	
	<!-- 引入配置文件 -->  
    <bean id="propertyConfigurer"  
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="location" value="classpath:mysql.properties" />  
    </bean>  
    
	<bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="${driver}" />  
        <property name="url" value="#{url}" />
        <property name="username" value="${username}" />  
        <property name="password" value="${password}" />  
         
    </bean>  
	
	 <!-- spring自动扫描mybatis的mapper.xml -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>  
    </bean>  
  
    <!-- Mapper接口所在包名,Spring会自动查找其下的类 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.syx.customer.mapper" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
    </bean>  
  
    <!-- 事务管理-->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
	
</beans>

2.1、创建mysql.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC
username=root
password=root
initialSize=0
maxActive=20
maxIdle=20
minIdle=1
maxWait=60000

3、将spring-mybatis.xml import到spring-mvc.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: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.1.xsd
     http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">

	<!-- 扫描改包路径及其子包下的注解类 -->
	<context:component-scan base-package="com.syx.customer" />

	<!-- 采用默认的方式处理静态资源 -->
	<mvc:default-servlet-handler />

	<!-- 采用了注释方式的注解就要配置 -->
	<mvc:annotation-driven />

	<!-- 视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver"
		id="internalResourceViewResolver">
		<!-- 前缀 -->
		<property name="prefix" value="/WEB-INF/page/" />
		<!-- 后缀 -->
		<property name="suffix" value=".jsp" />
	</bean>
	
	<import resource="spring-mybatis.xml"/>
</beans>

4、在resource/mapper下创建UserMapper.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="com.syx.customer.mapper.UserMapper">
	<!--根据id查找用户信息-->
	<select id="findUserById" parameterType="string" resultType="com.syx.customer.model.UserModel">
		SELECT * FROM  t_user WHERE id=#{id}
	</select>
</mapper>
5、创建Mapper接口和Model类

package com.syx.customer.mapper;

import org.springframework.stereotype.Repository;

import com.syx.customer.model.UserModel;

/** 
 * 用户Mapper接口   
 *
 * @author sunyx 
 * @since JDK 1.8 
 */
@Repository
public interface UserMapper {
	
	/**    
	 * 根据id查找用户信息
	 *
	 * @author sunyx 
	 * @param id
	 * @return 
	 * @since JDK 1.8  
	 */ 
	UserModel findUserById(String id);
	
}
package com.syx.customer.model;

/** 
 * 用户Model   
 *
 * @author sunyx 
 * @since JDK 1.8 
 */
public class UserModel {
	
	/** id*/ 
	private String id;
	
	/** 姓名*/ 
	private String name;

	public String getId() {
		return id;
	}

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

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}	
}
6、创建Service接口和Service实现类并注入Mapper

package com.syx.customer.service;

/** 
 * 简单的一个业务接口    
 *
 * @author sunyx 
 * @since JDK 1.8 
 */
public interface SimpleService {
	
	/**    
	 * 给指定用户打招呼 
	 *
	 * @author sunyx 
	 * @param id
	 * @return 
	 * @since JDK 1.8  
	 */ 
	String sayHello2User(String id);
}
package com.syx.customer.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.syx.customer.mapper.UserMapper;
import com.syx.customer.service.SimpleService;

/** 
 * 简单业务逻辑的实现类
 *
 * @author sunyx 
 * @since JDK 1.8 
 */
@Service("simpleService")
public class SimpleServiceImpl implements SimpleService {
	
	@Autowired
	private UserMapper userMapper;

	public String sayHello2User(String id) {
		System.out.println("hello:"+userMapper.findUserById(id));
		return "ok";
	}

}

7、在Controller中注入Service

package com.syx.customer.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.syx.customer.service.SimpleService;

/** 
 * 第一个Controller类     
 *
 * @author sunyx 
 * @since JDK 1.8 
 */
@Controller
@RequestMapping("/hello")
public class HelloWordController {
	
	@Autowired
	private SimpleService SimpleService;
	
	@RequestMapping("/syx")
    public String syx(){ 
		SimpleService.sayHello2User("1");
        return "syx";
    }
}
8、就是这么简单




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值