1 导包
1.1 spring-webmvc : spring框架包(当然里面也包含springmvc)
1.2 mybatis : mybatis框架包
1.3 mybatis-spring : spring框架集成mybatis框架需要用到的包
1.4 spring-jdbc : springjdbc包
1.5 mysql:MySQL数据库驱动包
1.6 junit : 单元测试包
1.7 dbcp : 数据库连接池
1 <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"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>cn.xiangxu.spring</groupId> 4 <artifactId>mybatis03</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <dependencies> 7 <dependency> 8 <groupId>org.springframework</groupId> 9 <artifactId>spring-webmvc</artifactId> 10 <version>3.2.8.RELEASE</version> 11 </dependency> 12 <dependency> 13 <groupId>org.mybatis</groupId> 14 <artifactId>mybatis</artifactId> 15 <version>3.2.8</version> 16 </dependency> 17 <dependency> 18 <groupId>org.mybatis</groupId> 19 <artifactId>mybatis-spring</artifactId> 20 <version>1.2.3</version> 21 </dependency> 22 <dependency> 23 <groupId>org.springframework</groupId> 24 <artifactId>spring-jdbc</artifactId> 25 <version>3.2.8.RELEASE</version> 26 </dependency> 27 <dependency> 28 <groupId>mysql</groupId> 29 <artifactId>mysql-connector-java</artifactId> 30 <version>5.1.23</version> 31 </dependency> 32 <dependency> 33 <groupId>junit</groupId> 34 <artifactId>junit</artifactId> 35 <version>4.12</version> 36 </dependency> 37 <dependency> 38 <groupId>commons-dbcp</groupId> 39 <artifactId>commons-dbcp</artifactId> 40 <version>1.4</version> 41 </dependency> 42 </dependencies> 43 </project>
2 配置文件
只需要添加spring的配置文件,不需要添加mybatis的配置文件;直接将mybatis有关的配置在spring配置文件中完成
2.1 配置能读取含有数据库连接信息的properties文件
2.2 配置数据库连接池
2.3 配置SqlSessionFactoryBean
SqlSessionFactoryBean中包含了数据库连接池和映射文件的信息
配置SqlSessionFactoryBean的作用
通过SqlSessionFactoryBean来配置数据库连接池和映射文件位置
可以通过SqlSessionFactoryBean来创建SqlSession对象
2.4 配置MapperScannerConfigurer
该bean负责调用SqlSession的getMapper方法,然后将得到的对象放到spring容器里面(默认的id是首字母小写后的接口名,也可以使用@Repository进行重命名) -->> 即:将mapper映射器变成一个对象放到spring容器中使用
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 4 xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" 5 xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 6 xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util" 7 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 8 xsi:schemaLocation=" 9 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 10 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 11 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 12 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 13 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 14 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 15 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 16 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 17 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 18 19 <!-- 读取config.properties文件 --> 20 <util:properties id="config" 21 location="classpath:mysql.properties"/> 22 23 <!-- 配置连接池 --> 24 <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> 25 <property name="driverClassName" value="#{config.driverClassName}" /> 26 <property name="url" value="#{config.url}" /> 27 <property name="username" value="#{config.username}" /> 28 <property name="password" value="#{config.password}" /> 29 </bean> 30 31 <!-- 配置SqlSessionFactoryBean --> 32 <bean id="ssfb" class="org.mybatis.spring.SqlSessionFactoryBean"> 33 <!-- 指定连接池的位置 --> 34 <property name="dataSource" ref="ds"></property> 35 <!-- 指定映射文件的位置 --> 36 <property name="mapperLocations" value="classpath:cn/xiangxu/telecom/login/entity/*.xml"></property> 37 </bean> 38 39 <!-- 配置MapperScannerConfigurer --> 40 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> 41 <!-- Mapper映射器所在的包 --> 42 <property name="basePackage" value="cn.xiangxu.telecom.login.dao"></property> 43 <!-- 指定只有带该注解的映射器才会被扫描 --> 44 <property name="annotationClass" value="cn.xiangxu.telecom.login.annotations.MyBatisRepository"></property> 45 </bean> 46 47 48 49 50 </beans>
3 编写实体类
实体类的属性名最好和表的字段名保持一致
1 package cn.xiangxu.telecom.login.entity; 2 3 import java.io.Serializable; 4 5 public class Admin implements Serializable { 6 private Integer id; 7 private String name; 8 private String password; 9 private String gender; 10 11 public Integer getId() { 12 return id; 13 } 14 public void setId(Integer id) { 15 this.id = id; 16 } 17 public String getName() { 18 return name; 19 } 20 public void setName(String name) { 21 this.name = name; 22 } 23 public String getPassword() { 24 return password; 25 } 26 public void setPassword(String password) { 27 this.password = password; 28 } 29 public String getGender() { 30 return gender; 31 } 32 public void setGender(String gender) { 33 this.gender = gender; 34 } 35 36 public String toString() { 37 return "Admin [id=" + id + ", name=" + name + ", password=" + password + ", gender=" + gender + "]"; 38 } 39 40 }
4 编写映射文件
映射文件和相应的实体类一般放在同一个包里面
要求
映射文件的 namespace属性值必须是相应Mapper映射器的接口名(包括包名)
例如:namespace="cn.xiangxu.telecom.login.dao.AdminDao"
1 <?xml version="1.0" encoding="UTF-8" ?> 2 <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" 3 "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> 4 5 <mapper namespace="cn.xiangxu.telecom.login.dao.AdminDao"> 6 <insert id="insert" parameterType="cn.xiangxu.telecom.login.entity.Admin"> 7 <!-- 注意:parameterType的属性值一定要写上类名(基本类型除外) --> 8 INSERT INTO admin 9 (name, password, gender) 10 VALUES(#{name}, #{password}, #{gender}) 11 </insert> 12 13 <select id="findAll" resultType="cn.xiangxu.telecom.login.entity.Admin"> 14 SELECT * FROM admin 15 </select> 16 17 <select id="findByName" parameterType="String" resultType="cn.xiangxu.telecom.login.entity.Admin"> 18 SELECT * FROM admin 19 WHERE NAME = #{MYNAME} 20 </select> 21 22 <update id="modify" parameterType="cn.xiangxu.telecom.login.entity.Admin"> 23 UPDATE admin 24 SET password=#{password}, gender=#{gender} 25 WHERE name=#{name} 26 </update> 27 28 <delete id="deleteByName" parameterType="String"> 29 DELETE FROM admin 30 WHERE name=#{deleteName} 31 </delete> 32 33 <select id="findByName2" parameterType="String" resultType="Map"> 34 SELECT * FROM admin 35 WHERE name=#{selectName} 36 </select> 37 38 <!-- 如果实体类的属性名和表的字段名不一致,那么不一致的字段将返回null --> 39 <select id="findByName3" parameterType="String" resultType="cn.xiangxu.telecom.login.entity.Admin2"> 40 SELECT * FROM admin 41 WHERE name = #{selectName} 42 </select> 43 44 <!-- 利用sql的别名解决实体类的属性名和标的字段名不一致的问题 --> 45 <select id="findByName4" parameterType="String" resultType="cn.xiangxu.telecom.login.entity.Admin2"> 46 SELECT id, name, password word, gender gender1 FROM admin 47 WHERE name = #{selectName} 48 </select> 49 50 <!-- 使用resultMap元素解决实体类的属性名和标的字段名不一致的问题 --> 51 <resultMap type="cn.xiangxu.telecom.login.entity.Admin2" id="admin2Map"> 52 <result property="word" column="password"></result> 53 <result property="gender1" column="gender"></result> 54 </resultMap> 55 <select id="findByName5" parameterType="String" resultMap="admin2Map"> 56 SELECT * FROM admin 57 WHERE name=#{selectName} 58 </select> 59 60 61 62 63 64 </mapper>
5 编写Mapper映射器
要求
方法名必须和映射文件中的sqlId属性值保持一致
方法的参数类型要与映射文件的parameterType保持一致
方法的返回类型要与映射文件的resultType文件保持一致
1 package cn.xiangxu.telecom.login.dao; 2 3 import java.util.List; 4 import java.util.Map; 5 6 import cn.xiangxu.telecom.login.entity.Admin; 7 import cn.xiangxu.telecom.login.entity.Admin2; 8 9 public interface AdminDao { 10 public void insert(Admin admin); // 插入方法 11 public List<Admin> findAll(); // 查询所有方法 12 public Admin findByName(String name); // 限定查询方法 13 public void modify(Admin admin); // 修改方法 14 public void deleteByName(String name); // 删除方法 15 16 public Map<Object, Object> findByName2(String name); // 查询结果返回Map 的方法 17 public Admin2 findByName3(String name); // 实体类的属性名和表的字段名不一致的情况 18 public Admin2 findByName4(String name); 19 public Admin2 findByName5(String name); 20 }
6 spingMVC中文乱码
7 SpringMVC响应数据JSON转化问题
获取本博客源代码:点击前往