欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MyBatis简介与配置MyBatis+Spring+MySql的方法

程序员文章站 2024-03-01 17:44:58
1.1mybatis简介       mybatis 是一个可以自定义sql、存储过程和高级映射的持久层框架。mybat...

1.1mybatis简介

      mybatis 是一个可以自定义sql、存储过程和高级映射的持久层框架。mybatis 摒除了大部分的jdbc代码、手工设置参数和结果集重获。mybatis 只使用简单的xml 和注解来配置和映射基本数据类型、map 接口和pojo 到数据库记录。相对hibernate和apache ojb等“一站式”orm解决方案而言,mybatis 是一种“半自动化”的orm实现。

需要使用的jar包:mybatis-3.0.2.jar(mybatis核心包)。mybatis-spring-1.0.0.jar(与spring结合包)。
下载地址:



1.2mybatis+spring+mysql简单配置

1.2.1搭建spring环境

1,建立maven的web项目;

2,加入spring框架、配置文件;

3,在pom.xml中加入所需要的jar包(spring框架的、mybatis、mybatis-spring、junit等);

4,更改web.xml和spring的配置文件;

5,添加一个jsp页面和对应的controller;

6,测试。

可参照:。使用eclipse的maven构建springmvc项目

1.2.2建立mysql数据库

建立一个学生选课管理数据库。

表:学生表、班级表、教师表、课程表、学生选课表。

逻辑关系:每个学生有一个班级;每个班级对应一个班主任教师;每个教师只能当一个班的班主任;

使用下面的sql进行建数据库,先建立学生表,插入数据(2条以上)。

更多sql请下载项目源文件,在resource/sql中。

/* 建立数据库 */ 
create database student_manager; 
use student_manager; 
/***** 建立student表 *****/ 
create table student_tbl 
( 
 student_id   varchar(255) primary key, 
 student_name  varchar(10) not null, 
 student_sex  varchar(10), 
 student_birthday date, 
 class_id   varchar(255) 
); 
/*插入学生数据*/ 
insert into student_tbl (student_id, 
       student_name, 
       student_sex, 
       student_birthday, 
       class_id) 
 values (123456, 
   '某某某', 
   '女', 
   '1980-08-01', 
   121546 
   ) 

创建连接mysql使用的配置文件mysql.properties。

jdbc.driverclassname=com.mysql.jdbc.driver 
jdbc.url=jdbc:mysql://localhost:3306/student_manager?user=root&password=limingnihao&useunicode=true&characterencoding=utf-8

1.2.3搭建mybatis环境

顺序随便,现在的顺序是因为可以尽量的少的修改写好的文件。

1.2.3.1创建实体类: studententity

public class studententity implements serializable { 
 private static final long serialversionuid = 3096154202413606831l; 
 private classentity classentity; 
 private date studentbirthday; 
 private string studentid; 
 private string studentname; 
 private string studentsex; 
 public classentity getclassentity() { 
  return classentity; 
 } 
 public date getstudentbirthday() { 
  return studentbirthday; 
 } 
 public string getstudentid() { 
  return studentid; 
 } 
 public string getstudentname() { 
  return studentname; 
 } 
 public string getstudentsex() { 
  return studentsex; 
 } 
 public void setclassentity(classentity classentity) { 
  this.classentity = classentity; 
 } 
 public void setstudentbirthday(date studentbirthday) { 
  this.studentbirthday = studentbirthday; 
 } 
 public void setstudentid(string studentid) { 
  this.studentid = studentid; 
 } 
 public void setstudentname(string studentname) { 
  this.studentname = studentname; 
 } 
 public void setstudentsex(string studentsex) { 
  this.studentsex = studentsex; 
 } 
} 

1.2.3.2创建数据访问接口

student类对应的dao接口:studentmapper。

public interface studentmapper { 
 public studententity getstudent(string studentid); 
 public studententity getstudentandclass(string studentid); 
 public list<studententity> getstudentall(); 
 public void insertstudent(studententity entity); 
 public void deletestudent(studententity entity); 
 public void updatestudent(studententity entity); 
} 

1.2.3.3创建sql映射语句文件

student类的sql语句文件studentmapper.xml

resultmap标签:表字段与属性的映射。

select标签:查询sql。

<?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.manager.data.studentmapper"> 
 <resultmap type="studententity" id="studentresultmap"> 
  <id property="studentid" column="student_id"/> 
  <result property="studentname" column="student_name"/> 
  <result property="studentsex" column="student_sex"/> 
  <result property="studentbirthday" column="student_birthday"/> 
 </resultmap> 
 <!-- 查询学生,根据id --> 
 <select id="getstudent" parametertype="string" resulttype="studententity" resultmap="studentresultmap"> 
  <![cdata[ 
   select * from student_tbl st 
    where st.student_id = #{studentid} 
  ]]> 
 </select> 
 <!-- 查询学生列表 --> 
 <select id="getstudentall" resulttype="com.manager.data.model.studententity" resultmap="studentresultmap"> 
  <![cdata[ 
   select * from student_tbl 
  ]]> 
 </select> 
</mapper> 

1.2.3.4创建mybatis的mapper配置文件

在src/main/resource中创建mybatis配置文件:mybatis-config.xml。

typealiases标签:给类起一个别名。com.manager.data.model.studententity类,可以使用studententity代替。

mappers标签:加载mybatis中实体类的sql映射语句文件。

<?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> 
 <typealiases> 
  <typealias alias="studententity" type="com.manager.data.model.studententity"/> 
 </typealiases> 
 <mappers> 
  <mapper resource="com/manager/data/maps/studentmapper.xml" />
 </mappers> 
</configuration> 

1.2.3.5修改spring 的配置文件

主要是添加sqlsession的制作工厂类的bean:sqlsessionfactorybean,(在mybatis.spring包中)。需要指定配置文件位置和datasource。

和数据访问接口对应的实现bean。通过mapperfactorybean创建出来。需要执行接口类全称和sqlsession工厂bean的引用。

<!-- 导入属性配置文件 --> 
<context:property-placeholder location="classpath:mysql.properties" /> 
<bean id="datasource" class="org.springframework.jdbc.datasource.drivermanagerdatasource"> 
 <property name="driverclassname" value="${jdbc.driverclassname}" /> 
 <property name="url" value="${jdbc.url}" /> 
</bean> 
<bean id="transactionmanager" class="org.springframework.jdbc.datasource.datasourcetransactionmanager"> 
 <property name="datasource" ref="datasource" /> 
</bean> 
<bean id="sqlsessionfactory" class="org.mybatis.spring.sqlsessionfactorybean"> 
 <property name="configlocation" value="classpath:mybatis-config.xml" /> 
 <property name="datasource" ref="datasource" /> 
</bean> 
<!— mapper bean --> 
<bean id="studentmapper" class="org.mybatis.spring.mapperfactorybean"> 
 <property name="mapperinterface" value="com.manager.data.studentmapper" /> 
 <property name="sqlsessionfactory" ref="sqlsessionfactory" /> 
</bean> 

也可以不定义mapper的bean,使用注解:

 将studentmapper加入注解

@repository 
@transactional 
public interface studentmapper { 
} 

对应的需要在dispatcher-servlet.xml中加入扫描:

<bean class="org.mybatis.spring.mapper.mapperscannerconfigurer"> 
 <property name="annotationclass" value="org.springframework.stereotype.repository"/> 
 <property name="basepackage" value="com.liming.manager"/> 
 <property name="sqlsessionfactory" ref="sqlsessionfactory"/> 
</bean> 

1.2.4测试studentmapper

使用springmvc测试,创建一个testcontroller,配置tomcat,访问index.do页面进行测试:

@controller 
public class testcontroller { 
 @autowired 
 private studentmapper studentmapper; 
 @requestmapping(value = "index.do") 
 public void indexpage() {  
  studententity entity = studentmapper.getstudent("10000013"); 
  system.out.println("name:" + entity.getstudentname()); 
 }  
} 

使用junit测试:

使用junit测试: 

java代码 

@runwith(value = springjunit4classrunner.class) 
@contextconfiguration(value = "test-servlet.xml") 
public class studentmappertest { 
 @autowired 
 private classmapper classmapper; 
 @autowired 
 private studentmapper studentmapper; 
 @transactional 
 public void getstudenttest(){ 
  studententity entity = studentmapper.getstudent("10000013"); 
  system.out.println("" + entity.getstudentid() + entity.getstudentname()); 
  list<studententity> studentlist = studentmapper.getstudentall(); 
  for( studententity entitytemp : studentlist){ 
   system.out.println(entitytemp.getstudentname()); 
  } 
 } 
} 

以上所述是小编给大家介绍的mybatis简介与配置mybatis+spring+mysql的方法,希望对大家有所帮助