您好,欢迎访问一九零五行业门户网

一、MyBatis简介与配置MyBatis+Spring+MySql

mybatis学习 之 一、mybatis简介与配置mybatis+spring+mysql mybatis学习 之 二、sql语句映射文件(1)resultmap mybatis学习 之 二、sql语句映射文件(2)增删改查、参数、缓存 mybatis学习 之 三、动态sql语句 mybatis学习 之 四、mybatis配置文件 1.1mybatis
mybatis学习 之 一、mybatis简介与配置mybatis+spring+mysql
mybatis学习 之 二、sql语句映射文件(1)resultmap
mybatis学习 之 二、sql语句映射文件(2)增删改查、参数、缓存
mybatis学习 之 三、动态sql语句
mybatis学习 之 四、mybatis配置文件
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结合包)。
下载地址:
http://ibatis.apache.org/tools/ibator
http://code.google.com/p/mybatis/
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,测试。
可参照:http://limingnihao.iteye.com/blog/830409。使用eclipse的maven构建springmvc项目
1.2.2建立mysql数据库建立一个学生选课管理数据库。
表:学生表、班级表、教师表、课程表、学生选课表。
逻辑关系:每个学生有一个班级;每个班级对应一个班主任教师;每个教师只能当一个班的班主任;
使用下面的sql进行建数据库,先建立学生表,插入数据(2条以上)。
更多sql请下载项目源文件,在resource/sql中。
sql代码 
/* 建立数据库 */  
createdatabase student_manager;  
use student_manager;
/***** 建立student表 *****/  
createtable student_tbl  
(  
  student_id         varchar(255) primarykey,  
  student_name       varchar(10) notnull,  
  student_sex        varchar(10),  
  student_birthday   date,  
  class_id           varchar(255)  
);
/*插入**/  
insertinto student_tbl (student_id,  
                        student_name,  
                        student_sex,  
                        student_birthday,  
                        class_id)  
values   (123456,  
'某某某',  
'女',  
'1980-08-01',  
           121546  
           )
创建连接mysql使用的配置文件mysql.properties。
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
java代码 
publicclass studententity implements serializable {
privatestaticfinallong 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;  
   }
publicvoid setclassentity(classentity classentity) {  
this.classentity = classentity;  
   }
publicvoid setstudentbirthday(date studentbirthday) {  
this.studentbirthday = studentbirthday;  
   }
publicvoid setstudentid(string studentid) {  
this.studentid = studentid;  
   }
publicvoid setstudentname(string studentname) {  
this.studentname = studentname;  
   }
publicvoid setstudentsex(string studentsex) {  
this.studentsex = studentsex;  
   }  
}
1.2.3.2创建数据访问接口student类对应的dao接口:studentmapper。
java代码 
publicinterface studentmapper {
public studententity getstudent(string studentid);
public studententity getstudentandclass(string studentid);
public list getstudentall();
publicvoid insertstudent(studententity entity);
publicvoid deletestudent(studententity entity);
publicvoid updatestudent(studententity entity);  
}
1.2.3.3创建sql映射语句文件
student类的sql语句文件studentmapper.xml
xml代码 
xmlversion=1.0encoding=utf-8?>
span>>
mappernamespace=com.manager.data.studentmapper>
resultmaptype=studententityid=studentresultmap>
idproperty=studentidcolumn=student_id/>
resultproperty=studentnamecolumn=student_name/>
resultproperty=studentsexcolumn=student_sex/>
resultproperty=studentbirthdaycolumn=student_birthday/>
resultmap>
selectid=getstudentparametertype=stringresulttype=studententityresultmap=studentresultmap>
select * from student_tbl st
               where st.student_id = #{studentid}
       ]]>
select>
selectid=getstudentallresulttype=com.manager.data.model.studententityresultmap=studentresultmap>
select * from student_tbl
       ]]>
select>
mapper>
1.2.3.4创建mybatis的mapper配置文件在src/main/resource中创建mybatis配置文件:mybatis-config.xml。
xml代码 
xmlversion=1.0encoding=utf-8?>
span>>
configuration>
typealiases>
typealiasalias=studententitytype=com.manager.data.model.studententity/>
typealiases>
mappers>
mapperresource=com/manager/data/maps/studentmapper.xml/>
mappers>
configuration>
1.2.3.5修改spring 的配置文件主要是添加sqlsession的制作工厂类的bean:sqlsessionfactorybean,(在mybatis.spring包中)。需要指定配置文件位置和datasource。
和数据访问接口对应的实现bean。通过mapperfactorybean创建出来。需要执行接口类全称和sqlsession工厂bean的引用。
xml代码
context:property-placeholderlocation=classpath:mysql.properties/>
beanid=datasourceclass=org.springframework.jdbc.datasource.drivermanagerdatasource>
propertyname=driverclassnamevalue=${jdbc.driverclassname}/>
propertyname=urlvalue=${jdbc.url}/>
bean>
beanid=transactionmanagerclass=org.springframework.jdbc.datasource.datasourcetransactionmanager>
propertyname=datasourceref=datasource/>
bean>
beanid=sqlsessionfactoryclass=org.mybatis.spring.sqlsessionfactorybean>
propertyname=configlocationvalue=classpath:mybatis-config.xml/>
propertyname=datasourceref=datasource/>
bean>
>
beanid=studentmapperclass=org.mybatis.spring.mapperfactorybean>
propertyname=mapperinterfacevalue=com.manager.data.studentmapper/>
propertyname=sqlsessionfactoryref=sqlsessionfactory/>
bean>
也可以不定义mapper的bean,使用注解:
将studentmapper加入注解
java代码 
@repository
@transactional
publicinterface studentmapper {  
}
对应的需要在dispatcher-servlet.xml中加入扫描:
xml代码 
beanclass=org.mybatis.spring.mapper.mapperscannerconfigurer>
propertyname=annotationclassvalue=org.springframework.stereotype.repository/>
propertyname=basepackagevalue=com.liming.manager/>
propertyname=sqlsessionfactoryref=sqlsessionfactory/>
bean>
1.2.4测试studentmapper使用springmvc测试,创建一个testcontroller,配置tomcat,访问index.do页面进行测试:
java代码 
@controller
publicclass testcontroller {
@autowired
private studentmapper studentmapper;
@requestmapping(value = index.do)  
publicvoid indexpage() {    
       studententity entity = studentmapper.getstudent(10000013);  
       system.out.println(name: + entity.getstudentname());  
   }    
}
使用junit测试:
java代码 
使用junit测试:  
java代码  
@runwith(value = springjunit4classrunner.class)  
@contextconfiguration(value = test-servlet.xml)  
publicclass studentmappertest {
@autowired
private classmapper classmapper;
@autowired
private studentmapper studentmapper;
@transactional
publicvoid getstudenttest(){  
       studententity entity = studentmapper.getstudent(10000013);  
       system.out.println( + entity.getstudentid() + entity.getstudentname());
list studentlist = studentmapper.getstudentall();  
for( studententity entitytemp : studentlist){  
           system.out.println(entitytemp.getstudentname());  
       }
}  
}
其它类似信息

推荐信息