一、MyBatis简介与配置MyBatis+Spring+MySql1.1MyBatis简介MyBatis是一个可以自定义SQL、存储过程和高级映射的持久层框架。MyBatis摒除了大部分的JDBC代码、手工设置参数和结果集重获。MyBatis只使用简单的XML和注解来配置和映射基本数据类型、Map接口和POJO到数据库记录。相对Hibernate和ApacheOJB等“一站式”ORM解决方案而言,Mybatis是一种“半自动化”的ORM实现。需要使用的Jar包:mybatis-3.0.2.jar(mybatis核心包)。mybatis-spring-1.0.0.jar(与Spring结合包)。下载地址:://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,测试。可参照:。使用Eclipse的Maven构建SpringMVC项目1.2.2建立MySql数据库建立一个学生选课管理数据库。表:学生表、班级表、教师表、课程表、学生选课表。逻辑关系:每个学生有一个班级;每个班级对应一个班主任教师;每个教师只能当一个班的班主任;使用下面的sql进行建数据库,先建立学生表,插入数据(2条以上)。更多sql请下载项目源文件,在resource/sql中。Sql代码1./*建立数据库*/2.CREATEDATABASESTUDENT_MANAGER;3.USESTUDENT_MANAGER;4.5./*****建立student表*****/6.CREATETABLESTUDENT_TBL7.(8.STUDENT_IDVARCHAR(255)PRIMARYKEY,9.STUDENT_NAMEVARCHAR(10)NOTNULL,10.STUDENT_SEXVARCHAR(10),11.STUDENT_BIRTHDAYDATE,12.CLASS_IDVARCHAR(255)13.);14.15./*插入学生数据*/16.INSERTINTOSTUDENT_TBL(STUDENT_ID,17.STUDENT_NAME,18.STUDENT_SEX,19.STUDENT_BIRTHDAY,20.CLASS_ID)21.VALUES(123456,22.'某某某',23.'女',24.'1980-08-01',25.12154626.)创建连接MySql使用的配置文件mysql.properties。Mysql.properties代码1.jdbc.driverClassName=com.mysql.jdbc.Driver2.jdbc.url=jdbc:mysql://localhost:3306/student_manager?user=root&password=limingnihao&useUnicode=true&characterEncoding=UTF-81.2.3搭建MyBatis环境顺序随便,现在的顺序是因为可以尽量的少的修改写好的文件。1.2.3.1创建实体类:StudentEntityJava代码1.publicclassStudentEntityimplementsSerializable{2.3.privatestaticfinallongserialVersionUID=3096154202413606831L;4.privateClassEntityclassEntity;5.privateDatestudentBirthday;6.privateStringstudentID;7.privateStringstudentName;8.privateStringstudentSex;9.10.publicClassEntitygetClassEntity(){11.returnclassEntity;12.}13.14.publicDategetStudentBirthday(){15.returnstudentBirthday;16.}17.18.publicStringgetStudentID(){19.returnstudentID;20.}21.22.publicStringgetStudentName(){23.returnstudentName;24.}25.26.publicStringgetStudentSex(){27.returnstudentSex;28.}29.30.publicvoidsetClassEntity(ClassEntityclassEntity){31.this.classEntity=classEntity;32.}33.34.publicvoidsetStudentBirthday(DatestudentBirthday){35.this.studentBirthday=studentBirthday;36.}37.38.publicvoidsetStudentID(StringstudentID){39.this.studentID=studentID;40.}41.42.publicvoidsetStudentName(StringstudentName){43.this.studentName=studentName;44.}45.46.publicvoidsetStudentSex(StringstudentSex){47.this.studentSex=studentSex;48.}49.}1.2.3.2创建数据访问接口Student类对应的dao接口:StudentMapper。Java代码1.publicinterfaceStudentMapper{2.3.publicStudentEntitygetStudent(StringstudentID);4.5.publicStudentEntitygetStudentAndClass(StringstudentID);6.7.publicListStudentEntitygetStudentAll();8.9.publicvoidinsertStudent(StudentEntityentity);10.11.publicvoiddeleteStudent(StudentEntityentity);12.13.publicvoidupdateStudent(StudentEntityentity);14.}1.2.3.3创建SQL映射语句文件Student类的sql语句文件StudentMapper.xmlresultMap标签:表字段与属性的映射。Select标签:查询sql。Xml代码1.?xmlversion=1.0encoding=UTF-8?2.!DOCTYPEmapperPUBLIC-//mybatis.org//DTDMapper3.0//EN=com.manager.data.StudentMapper4.5.resultMaptype=StudentEntityid=studentResultMap6.idproperty=studentIDcolumn=STUDENT_ID/7.resultproperty=studentNamecolumn=STUDENT_NAME/8.resultproperty=studentSexcolumn=STUDENT_SEX/9.resultproperty=studentBirthdaycolumn=STUDENT_BIRTHDAY/10./resultMap11.12.!--查询学生,根据id--13.selectid=getStudentparameterType=StringresultType=StudentEntityresultMap=studentResultMap14.![CDATA[15.SELECT*fromSTUDENT_TBLST16.WHEREST.STUDENT_ID=#{studentID}17.]]18./select19.20.!--查询学生列表--21.selectid=getStudentAllresultType=com.manager.data.model.StudentEntityresultMap=studentResultMap22.![CDATA[23.SELECT*fromSTUDENT_TBL24.]]25./select26.27./mapper1.2.3.4创建MyBatis的mapper配置文件在src/main/resource中创建MyBatis配置文件:mybatis-config.xml。typeAliases标签:给类起一个别名。com.manager.data.model.StudentEntity类,可以使用StudentEntity代替。Mappers标签:加载MyBatis中实体类的SQL映射语句文件。Xml代码1.?xmlversion=1.0encoding=UTF-8?2.!DOCTYPEconfigurationPUBLIC-//mybatis.org//DTDConfig3.0//EN=StudentEntitytype=com.manager.data.model.StudentEntity/6./typeAliases7.mappers8.mapperresource=com/manager/data/maps/StudentMapper.xml/9./mappers10./configuration1.2.3.5修改Spring的配置文件主要是添加SqlSession的制作工厂类的bean:SqlSessionFactoryBean,(在mybatis.spring包中)。需要指定配置文件位置和dataSource。和数据访问接口对应的实现bean。通过MapperFactoryBean创建出来。需要执行接口类全称和SqlSession工厂bean的引用。Xml代码1.!--导入属性配置文件--2.context:property-placeholderlocation=classpath:mysql.properties/3.4.beanid=dataSourceclass=org.springframework.jdbc.datasource.DriverManagerDataSource5.propertyname=driverClassNamevalue=${jdbc.driverClassName}/6.propertyname=urlvalue=${jdbc.url}/7./bean8.9.beanid=transactionManagerclass=org.springframework.jdbc.datasource.DataSourceTransactionManager