MyBatis学习 之 一、MyBatis简介与配置MyBaits Spring MySql.doc

上传人:啊飒飒 文档编号:10390208 上传时间:2021-05-14 格式:DOC 页数:8 大小:81.50KB
返回 下载 相关 举报
MyBatis学习 之 一、MyBatis简介与配置MyBaits Spring MySql.doc_第1页
第1页 / 共8页
MyBatis学习 之 一、MyBatis简介与配置MyBaits Spring MySql.doc_第2页
第2页 / 共8页
MyBatis学习 之 一、MyBatis简介与配置MyBaits Spring MySql.doc_第3页
第3页 / 共8页
MyBatis学习 之 一、MyBatis简介与配置MyBaits Spring MySql.doc_第4页
第4页 / 共8页
MyBatis学习 之 一、MyBatis简介与配置MyBaits Spring MySql.doc_第5页
第5页 / 共8页
点击查看更多>>
资源描述

《MyBatis学习 之 一、MyBatis简介与配置MyBaits Spring MySql.doc》由会员分享,可在线阅读,更多相关《MyBatis学习 之 一、MyBatis简介与配置MyBaits Spring MySql.doc(8页珍藏版)》请在三一文库上搜索。

1、一、MyBatis简介与配置MyBaits+Spring+MySql1.1MyBatis简介MyBatis 是一个可以自定义SQL、存储过程和高级映射的持久层框架。MyBatis 摒除了大部分的JDBC代码、手工设置参数和结果集重获。MyBatis 只使用简单的XML 和注解来配置和映射基本数据类型、Map 接口和POJO 到数据库记录。相对Hibernate和Apache OJB等“一站式”ORM解决方案而言,ibatis 是一种“半自动化”的ORM实现。需要使用的Jar包:mybatis-3.0.2.jar(mybatis核心包)。mybatis-spring-1.0.0.jar(与Spr

2、ing结合包)。下载地址:http:/ibatis.apache.org/tools/ibatorhttp:/ 建立数据库 */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);

3、/*插入学生数据*/INSERT INTO STUDENT_TBL (STUDENT_ID, STUDENT_NAME, STUDENT_SEX, STUDENT_BIRTHDAY, CLASS_ID) VALUES (123456, 某某某, 女, 1980-08-01, 121546 )创建连接MySql使用的配置文件mysql.properties。mysql.propertiesjdbc.driverClassName=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql:/localhost:3306/student_manager?user=root&p

4、assword=limingnihao&useUnicode=true&characterEncoding=UTF-81.2.3搭建MyBatis环境 顺序随便,现在的顺序是因为可以尽量的少的修改写好的文件。1.2.3.1创建实体类: StudentEntityJava代码public class StudentEntity implements Serializable private static final long serialVersionUID = 3096154202413606831L;private ClassEntity classEntity;private Date s

5、tudentBirthday;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;

6、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

7、void setStudentName(String studentName) this.studentName = studentName;public void setStudentSex(String studentSex) this.studentSex = studentSex;1.2.3.2创建数据访问接口Student类对应的dao接口:StudentMapper。Java代码public interface StudentMapper public StudentEntity getStudent(String studentID);public StudentEntity g

8、etStudentAndClass(String studentID);public List getStudentAll();public void insertStudent(StudentEntity entity);public void deleteStudent(StudentEntity entity);public void updateStudent(StudentEntity entity);1.2.3.3创建SQL映射语句文件Student类的sql语句文件StudentMapper.xmlresultMap标签:表字段与属性的映射。Select标签:查询sql。SELE

9、CT * from STUDENT_TBL STWHERE ST.STUDENT_ID = #studentID SELECT * from STUDENT_TBL1.2.3.4创建MyBatis的mapper配置文件在src/main/resource中创建MyBatis配置文件:mybatis-config.xml。typeAliases标签:给类起一个别名。com.manager.data.model.StudentEntity类,可以使用StudentEntity代替。Mappers标签:加载MyBatis中实体类的SQL映射语句文件。XML代码1.2.3.5修改Spring 的配置文

10、件主要是添加SqlSession的制作工厂类的bean:SqlSessionFactoryBean,(在mybatis.spring包中)。需要指定配置文件位置和dataSource。和数据访问接口对应的实现bean。通过MapperFactoryBean创建出来。需要执行接口类全称和SqlSession工厂bean的引用。XML代码1.2.4测试StudentMapper使用SpringMVC测试,创建一个TestController测试:Java代码Controllerpublic class TestController Autowiredprivate StudentMapper st

11、udentMapper;RequestMapping(value = index.do)public void indexPage() StudentEntity entity = studentMapper.getStudent(10000013);System.out.println(name: + entity.getStudentName();使用Junit测试:Java代码RunWith(value = SpringJUnit4ClassRunner.class)ContextConfiguration(value = test-servlet.xml)public class St

12、udentMapperTest Autowiredprivate ClassMapper classMapper;Autowiredprivate StudentMapper studentMapper;Transactionalpublic void 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();标签性语言XML代码MXML代码XAML代码开发语言Java代码C代码C+代码C#代码ActionScript3.0代码Web开发语言JavaScrtipt代码HTML代码代码Jsp代码样式CSS代码数据库SQL代码

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1