SSM框架简介

SSM=Spring+SpringMvc+Mybatis

S: Spring
一款轻量级框架他最大的两大特点1.控制反转(IOC) 2.面向切面(AOP)
1.控制反转(IOC):应用此框架可以通过注解将自己编写的bean(也就是service、dao等的实体类)注入入到spring容器,每当自己使用的时候也可以通过注解将注入容器的bean从容器中取出,实现了松散耦合,也就是通常说的解耦合。
2.面向切面(AOP):通常我们所编写的代码从jsp、html⬅controller⬅service⬅dao、mapper这一连串的逻辑属于纵向的逻辑,面向切面就是将这些纵向的主要逻辑横向扩展,无关乎业务的主要逻辑,可以通过注解和实体类两种方式将事务管理、日志输出、监控等切入主业务而不影响主业务。(要是小白的话这里不容易理解,要是有需要我详细解释的话留言给我、我单独解释)

S: SpringMVC
SpringMVC是Spring的一个实现了MVC模式的web模块,主要的作用是接收用户前端页面输入的请求,SpringMVC通过他的dispatcherServlet来将此请求分发到可以处理他的处理器中,经过处理将所得数据和所展示的页面返回,展示给用户。
M: Mybatis
了解这个,先得知道ORM(Object Relational Mapping)对象关系映射,
Object:指代面向对象的编程语言,java就是体量最大的一种。
Relational :指代关系型数据库,例如Mysql、Oracle等。
Mapping:Object和Relational之间的转换。

ORM就是为了设计初衷就是为了使对象能方便转化成关系(就是关系型数据库中的一行)来存储在数据库中,Mybatis就是ORM的一种实现。

简介就到这里,咱们主要是来搭建的,粗发!

标题本工程简介

JDK版本:1.8 IDE:eclipse tomcat7 依赖导入方式:maven仓库

工程结构

在这里插入图片描述

1.pom.xml依赖引入

将所需的依赖从maven仓库引入

<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.fengxiaobin</groupId>
	<artifactId>TEST-SSM</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>war</packaging>
	<name>SSM</name>
	<description>test ssm</description>
	<build>
		<plugins>
			<plugin>
				<!-- 配置插件 -->
				<groupId>org.apache.tomcat.maven</groupId>
				<artifactId>tomcat7-maven-plugin</artifactId>
				<configuration>
					<port>8080</port>
					<path>/</path>
					<uriEncoding>UTF-8</uriEncoding>
				</configuration>
			</plugin>
		</plugins>
	</build>
	<properties>
		<!-- 设置项目编码编码 -->
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<!-- spring版本号 -->
		<spring.version>4.1.3.RELEASE</spring.version>
		<!-- mybatis版本号 -->
		<mybatis.version>3.5.1</mybatis.version>
	</properties>
	<dependencies>
		<!-- java ee -->
		<dependency>
			<groupId>javax</groupId>
			<artifactId>javaee-api</artifactId>
			<version>7.0</version>
		</dependency> 
		<!--解決Unable to create a Configuration, because no Bean Validation provider could be found. Add a provider like Hibernate Validator (RI) to your classpath.  -->
		<dependency>  
			<groupId>org.hibernate</groupId>  
			<artifactId>hibernate-validator</artifactId>  
			<version>5.2.4.Final</version>  
		</dependency>  
		<!--jsp+servlet -->
		<dependency>
			<groupId>javax.servlet.jsp</groupId>
			<artifactId>jsp-api</artifactId>
			<version>2.1</version>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<version>3.1.0</version>
			<scope>provided</scope>
		</dependency>
		<!-- jstl -->
		<dependency>
			<groupId>jstl</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>
		<!-- 单元测试(scope为test表示依赖项目仅仅参与测试相关的工作,包括测试代码的编译,执行。) -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
			<scope>test</scope>
		</dependency>
		<!-- log4j -->
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.17</version>
		</dependency>
		<!-- fastjson -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>fastjson</artifactId>
			<version>1.2.51</version>
		</dependency>
		<!-- (1)在Maven项目中添加Oracle的驱动 -->
		<dependency>
			<groupId>com.oracle</groupId>
			<artifactId>ojdbc6</artifactId>
			<version>11.2.0.1.0</version>
		</dependency>
		<!-- (2)在Maven项目中添加Mysql的驱动 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.41</version>
			<scope>runtime</scope>
		</dependency>
		<!-- 数据库连接池(1) -->
		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>0.9.5.2</version>
		</dependency>
		<!-- 数据库连接池(2) -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.2.2</version>
		</dependency>
		<dependency>
			<groupId>commons-pool</groupId>
			<artifactId>commons-pool</artifactId>
			<version>1.6</version>
		</dependency>
		<!-- 数据库连接池(3) -->
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.0.9</version>
		</dependency>
		<!-- Spring -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-beans</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
		<groupId>org.springframework</groupId>
		<artifactId>spring-aop</artifactId>
		<version>${spring.version}</version>
	</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aspects</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-orm</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-tx</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-web</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
		</dependency>
		<!-- spring aop使用AspectJ的注解功能 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.7</version>
		</dependency>
		<!-- mybatis/spring整合包 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.1</version>
		</dependency>
		<!-- MyBatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>${mybatis.version}</version>
		</dependency>
		<!-- MyBatis的pagehelper分页插件 -->
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper</artifactId>
			<version>5.0.0</version>
		</dependency>
		<!-- 文件上传 -->
		<dependency> 
		     <groupId>commons-fileupload</groupId> 
		     <artifactId>commons-fileupload</artifactId> 
		     <version>1.2.1</version> 
		</dependency> 
		<dependency> 
		     <groupId>org.apache.commons</groupId> 
		     <artifactId>commons-io</artifactId> 
		     <version>1.3.2</version> 
		</dependency> 
	</dependencies>
</project>

2.大容器的配置applicationContext.xml

这个里面主要配置了连接数据库的相关信息(解释后续补充)。.
(1)db.properties的配置(applicationContext.xml里面要用)

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/spring
name=root
password=123
<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 1.扫描业务层的基本包(容器自动扫描IOC组件) -->
   	<context:component-scan base-package="com.fengxiaobin.service"></context:component-scan>

	<!-- 2.将db.properties引入(配置数据库相关参数) -->
	<context:property-placeholder location="classpath:db.properties" />	

	<!--(3)配置DRUID数据源    可选c3p0  或bdcp 这里我用的阿里的druid --可自行添加更详细配置例如最大、最小链接数、超时时间等>
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
		destroy-method="close">
		<property name="driverClassName" value="${driver}" />
		<property name="url" value="${url}" />
		<property name="username" value="${name}" />
		<property name="password" value="${password}" />		
	</bean>

	<!--3.会话工厂bean sqlSessionFactoryBean -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		 <!-- 注入数据库连接池 -->
		<property name="dataSource" ref="dataSource" />
		<!--  扫描pojo包 使用别名 -->
		<property name="typeAliasesPackage" value="com.fengxiaobin.pojo"></property>
		
		
		<!--   扫描sql配置文件:mapper需要的xml文件 
        <property name="mapperLocations" value="classpath:mapper/*.xml"/> -->
		<property name="configLocation" value="classpath:mybatis.xml"></property>
		
	</bean>

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

	<!-- 5.声明式事务管理 -->
	<!--定义事物管理器,由spring管理事务 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!--6.配置事务 (任选其一) -->
	<!--(1)事务的实现方式 1 -->
	 <tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="serviceMethod" expression="execution(* com..service..*.*(..))" />
		<aop:advisor pointcut-ref="serviceMethod" advice-ref="txAdvice" />
	</aop:config>


	<!--(2)事务的实现方式 2 配置基于注解的声明式事务 -->
	<!-- <tx:annotation-driven transaction-manager="transactionManager"/> -->

	<!--7. aspectj支持自动代理实现AOP功能 -->
	<!-- <aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy> -->
</beans> 

3.小容器的配置SpringMVC.xml

这个主要为通过SpringMVC的前端控制器DispatcherServlet来分发从前端接收的请求(更多解释后续补充)。

<?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:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop" 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/tx
           http://www.springframework.org/schema/tx/spring-tx.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd
           http://www.springframework.org/schema/mvc
           http://www.springframework.org/schema/mvc/spring-mvc.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
	<!-- 1.(扫描web相关的bean)配置扫描的基本包 -->
	<context:component-scan base-package="com.fengxiaobin.controller"></context:component-scan>
	<!-- 2.(开启SpringMVC注解模式)anno启动注解驱动 (静态资源处理)当springmvc处理不了时,用tomcat处理 -->
	<mvc:annotation-driven />
	<!-- 3.(静态资源默认servlet配置 )默认的servlet处理器tomcat 当遇到DispatcherServlet处理不了静态资源时由tomcat处理 -->
	<mvc:default-servlet-handler />

	<!-- 4.(配置jsp 显示ViewResolver)配置视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="viewClass"
			value="org.springframework.web.servlet.view.JstlView"></property>
		<property name="prefix" value="/"></property>
		<!--较为安全写法
		 <property name="prefix" value="/WEB-INF/views/"></property> -->
		<property name="suffix" value=".jsp"></property>
	</bean>
	
	<!-- 文件上传  -->
	<bean id="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<property name="maxUploadSize">
			<value>999999999999999</value>
		</property>
	</bean>
</beans>

4.配置Mybatis.xml

Mybatis是对象关系映射(orm)的实现,用来操作数据库,可将对象封装的数据转化成关系型数据库所需要的格式来存储数据。

<?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>
	<plugins>
		<!-- com.github.pagehelper为PageHelper类所在包名(要是使用pageHelp分页插件的话可以在这里配置下) -->
		<plugin interceptor="com.github.pagehelper.PageInterceptor">
			<!-- 什么都不配,使用默认的配置 -->
		</plugin>
	</plugins>
</configuration>

5.配置web.xml使刚才的配置启动时加载

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance " xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
  <display-name>TEST-SSM</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <!-- 编码过滤器 -->
    <filter>
        <filter-name>encodingFilter</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>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
     <!-- 1.配置监听加载applicationContext大容器 -->
     <listener>
	 <!--ContextLoaderListener是整个spring的上下文 ,加载log、 Service Dao(Mapper) DataSource ,Transaction,propertiesLoader,etc -->
	  	<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>
	 
    <!-- 2.加载springMVC的小容器(配置DispatcherServlet)-->
    <!--DispatcherServlet是springmvc的上下文,加载controller、ViewResolver、MultipartResolver、
    ExceptionHandler、HanderMapping、HanderAdapter、web层组件  -->
     <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>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>SpringMVC</servlet-name>
        <!-- 匹配所有请求 -->
        <url-pattern>/</url-pattern>
    </servlet-mapping>

</web-app>

6.配置log4j来输出日志

log4j.rootLogger=DEBUG, Console 

#Console 
log4j.appender.Console=org.apache.log4j.ConsoleAppender 
log4j.appender.Console.layout=org.apache.log4j.PatternLayout 
log4j.appender.Console.layout.ConversionPattern=%d [%t] %-5p [%c] - %m%n 

log4j.logger.java.sql.ResultSet=INFO 
log4j.logger.org.apache=INFO 
log4j.logger.java.sql.Connection=DEBUG 
log4j.logger.java.sql.Statement=DEBUG 
log4j.logger.java.sql.PreparedStatement=DEBUG

至此配置已完成 接下来测试
再添加一张工程结构免得你们翻上去看
在这里插入图片描述

1.在Mysql数据库建立一张表

表结构如图所示
在这里插入图片描述

2.建立jsp页面index.jsp

此页面来让用户输入数据

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
     <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%-- <link href="${pageContext.request.contextPath}">--%>
 <title>Insert title here</title>
<script type="text/javascript">
	function query(){
		var input_id=document.getElementById("id").value;
		alert(input_id);
		location.href="selById?id="+input_id;
	}
</script>
</head>
<body>
<!-- <p>部门id<input id="input_id" type="text"/></p>
<p><input type="button" value="获取" onclick="query()"/></p></br> -->
<form action="dept/selById" method="post" enctype="multipart/form-data">
	<p>部门id<input id="id" name="id" type="text"/></p>
	<p><input type="submit" value="获取" /></p>
</form><br/>
<p><input type="submit" value="获取2" onclick="query()"/></p>
<p>部门id:<input type="text" name="did" value="${dept.id}"></p>
<p>部门名称:<input type="text" name="dname" value="${dept.name}"></p>
</body>
</html>

3.编写实体类Dept.java

/**
 * 
 */
package com.fengxiaobin.pojo;

/**
 * @author fxb
 * @version **
 * ����ʱ�䣺2019��12��13������9:47:19<br>
 * ��������
 */
public class Dept {
private int id;
private String name;

/**
 * @param id
 * @param name
 */
public Dept(int id, String name) {
	super();
	this.id = id;
	this.name = name;
}
/**
 * 
 */
public Dept() {
	super();
	// TODO Auto-generated constructor stub
}
/**
 * @return the id
 */
public int getId() {
	return id;
}
/**
 * @param id the id to set
 */
public void setId(int id) {
	this.id = id;
}
/**
 * @return the name
 */
public String getName() {
	return name;
}
/**
 * @param name the name to set
 */
public void setName(String name) {
	this.name = name;
}
/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
	return "Dept [id=" + id + ", name=" + name + "]";
}

}

4.编写Controller来接受用户的数据

注解解释:
@Controller 使Spring容器将此Bean标记为Controller并注入Spring容器,由Spring容器来管理此bean
@RequestMapping("/dept") 请求地址映射注解项目工程名后加/dept可以找到此bean
@Autowired将注入容器的bean拉取到此bean中(可大致看成DeptService s=new DeptService();

/**
 * 
 */
package com.fengxiaobin.controller;

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

import com.fengxiaobin.pojo.Dept;
import com.fengxiaobin.service.DeptService;

/**
 * @author fxb
 * @version ** ����ʱ�䣺2019��12��13������10:50:01<br>
 *          ��������
 */
@Controller
@RequestMapping("/dept")
public class DeptController {
	@Autowired
	private DeptService s;

	@RequestMapping("/selById")
	// @RequestParam("name") String name
	public String selById(Dept d, Model model) {
		Dept dept = s.selById(d.getId());
		model.addAttribute("dept", dept);
		return "index";
	}
}

5.编写Service层来添加业务代码

注解解释:
@Service 将此bean标记为service bean并纳入spring容器,交给spring容器管理

/**
 * 
 */
package com.fengxiaobin.service;

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

import com.fengxiaobin.mapper.DeptMapper;
import com.fengxiaobin.pojo.Dept;

/**
 * @author fxb
 * @version **
 * ����ʱ�䣺2019��12��13������10:52:39<br>
 * ��������
 */
@Service
public class DeptService {
	@Autowired
	private DeptMapper m;

	public Dept selById(int id) {
		return m.selById(id);
	}
}

6.编写mapper接口和它对应的XML文件(这两个名字要相同)

(1)DeptMapper 接口

package com.fengxiaobin.mapper;

import com.fengxiaobin.pojo.Dept;

/**
 * @author fxb
 * @version **
 * ����ʱ�䣺2019��12��13������9:52:46<br>
 * ��������
 */
public interface DeptMapper {
	public Dept selById(int id);
}

(2)DeptMapper.xml 编写sql语句
resultType:返回的结果类型
parameterType:参数类型

<?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.fengxiaobin.mapper.DeptMapper">
	<select id="selById" resultType="Dept" parameterType="int">
		select * from dept where id=#{id}
	</select>
</mapper>

结束,运行测试
右击index,jsp运行
部门id输入2,点击获取或者获取2得到部门id和部门名称
测试结果如下
在这里插入图片描述
动动大家的小手,点在点赞,祝大家新年发大财!

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐