1.2.2 Build MySQL Database
Under C:\Program files\mysql\mysql Server 5.7\bin:
First connect the Mysql:mysql-u root-p
/*set up a database*/CREATE DATABASE Student_manager; Use Student_manager; /** * * Create student Table * * * **/CREATE TABLE student_tbl (student_id VARCHAR (255) PRIMARY KEY, Student_name VARCHAR (Ten) not NULL, Student_sex VARCHAR (Ten), Student_birthday DATE, class_id VARCHAR (255) ); /*Insert student Data*/INSERT into Student_tbl (student_id, Student_name, Student_sex, Student_birthday, class_id) VALUES (123456, 'some xxx', 'female', '1980-08-01', 121546 )
Create a connection to MySQL using the configuration file mysql.properties.
Mysql.properties Code
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 Build MyBatis Environment 1.2.3.1 Create entity class: Studententity
Packagecom.deppon.test04.entity;Importjava.io.Serializable;Importjava.util.Date; Public classStudententityImplementsSerializable {Private Static Final LongSerialversionuid = 3096154202413606831L; PrivateDate Studentbirthday; PrivateString StudentID; PrivateString Studentname; PrivateString Studentsex; PublicDate Getstudentbirthday () {returnStudentbirthday; } PublicString Getstudentid () {returnStudentID; } PublicString Getstudentname () {returnStudentname; } PublicString Getstudentsex () {returnStudentsex; } Public voidSetstudentbirthday (Date studentbirthday) { This. Studentbirthday =Studentbirthday; } Public voidSetstudentid (String studentid) { This. StudentID =StudentID; } Public voidsetstudentname (String studentname) { This. Studentname =Studentname; } Public voidsetstudentsex (String studentsex) { This. Studentsex =Studentsex; } } 1.2.3.2 creating a data access interface
The student class corresponds to the DAO Interface: Studentmapper.
Public InterfaceStudentmapper { Publicstudententity getstudent (String studentid); Publicstudententity Getstudentandclass (String studentid); PublicList<studententity>Getstudentall (); Public voidinsertstudent (studententity entity); Public voiddeletestudent (studententity entity); Public voidupdatestudent (studententity entity); } 1.2.3.3 creating a SQL Map statement file
SQL statement file for the student class Studentmapper.xml
Resultmap Tags: mapping of table fields to properties.
Select tag: Query 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 "> <Mappernamespace= "Com.deppon.test04.dao.StudentMapper"> <Resultmaptype= "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> <!--query students, based on ID - <SelectID= "Getstudent"ParameterType= "String"Resulttype= "Com.deppon.test04.entity.StudentEntity"Resultmap= "Studentresultmap"> <! [Cdata[SELECT * from Student_tbl St WHERE St. student_id = #{studentid}]]> </Select> <!--Query Student List - <SelectID= "Getstudentall"Resulttype= "Com.deppon.test04.entity.StudentEntity"Resultmap= "Studentresultmap"> <! [Cdata[SELECT * from Student_tbl]]> </Select> </Mapper> 1.2.3.4 Creating a mybatis Mapper configuration file
Create the MyBatis configuration file in Src/main/resource: Mybatis-config.xml.
Typealiases Tag: An alias for the class. Com.manager.data.model.StudentEntity class, you can use studententity instead.
Mappers tag: Loads the SQL map statement file for the entity class in the MyBatis.
<?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" /c4>> <Configuration> <!--Global alias settings, only write aliases in the mapping file, without having to write out the entire classpath - <typealiases> <Typealiastype= "Com.deppon.test04.entity.StudentEntity"alias= "Studententity"/> </typealiases> <mappers> <MapperResource= "Src/main/resources/studentmapper.xml" /> </mappers> </Configuration>
1.2.3.5 Modifying the spring configuration file
The main is to add sqlsession to the production factory class Bean:sqlsessionfactorybean, (in the mybatis.spring package). You need to specify the configuration file location and datasource.
and the implementation bean corresponding to the data access interface. Created by Mapperfactorybean. A reference to the interface class name and Sqlsession factory Bean needs to be executed
MyBatis Learning two MyBatis introduction and configuration Mybatis+spring+mysql