One of MyBatis learning, MyBatis introduction and MyBatis configuration MyBatis + Spring + MySqlMyBatis learning 2, SQL statement ing file (1) resultMapMyBatis learning 2, SQL statement ing file (2) add, delete, modify, query, parameter, cache MyBatis learning 3, dynamic SQL statement MyBatis learning 4, MyBatis configuration file 1.1 MyBatis
MyBatis Learning 1. MyBatis introduction and configuration MyBatis + Spring + MySql MyBatis learning 2. SQL statement ing file (1) resultMap MyBatis learning 2. SQL statement ing file (2) add, delete, modify, query, parameters, cache MyBatis learning 3. dynamic SQL statement MyBatis learning 4. MyBatis configuration file 1.1 MyBatis
MyBatis 1. Introduction and configuration of MyBatis + Spring + MySql
MyBatis 2. SQL statement ing file (1) resultMap
MyBatis 2. SQL statement ing file (2) addition, deletion, modification, query, parameter, and Cache
MyBatis 3. dynamic SQL statements
MyBatis learning 4. MyBatis configuration file
1.1MyBatis Overview
MyBatis is a persistent layer framework that allows you to customize SQL, stored procedures, and advanced mappings. MyBatis distinct removes most of the JDBC code, manual setting parameters, and result set re-acquisition. MyBatis only uses simple XML and annotations to configure and Map basic data types, Map interfaces, and POJO to database records. Compared with "one-stop" ORM solutions such as Hibernate and Apache OJB, Mybatis is a "semi-automated" ORM implementation.
Jar package to be used: mybatis-3.0.2.jar (mybatis core package ). Mybatis-spring-1.0.0.jar (combined with Spring ).
:
Http://ibatis.apache.org/tools/ibator
Http://code.google.com/p/mybatis/
1.2 MyBatis + Spring + MySql simple configuration 1.2.1 build a Spring Environment
1. Create a maven web project;
2. Add the Spring framework and configuration file;
3. Add the required jar packages (spring framework, mybatis, mybatis-spring, and junit) in pom. xml );
4. modify the configuration files of web. xml and spring;
5. Add a jsp page and the corresponding Controller;
6. Test.
See http://limingnihao.iteye.com/blog/830409.Use Maven of Eclipse to build the SpringMVC Project
1.2.2 create a MySql database
Create a course selection management database for students.
Table: Student table, class table, instructor table, Course table, and student course selection table.
Logical Relationship: Each student has a class; each class corresponds to a class teacher; each teacher can only be a class teacher;
Use the following SQL statement to create a database. First, create a student table and insert data (more than 2 data records ).
For more SQL statements, download the project source file in resource/SQL.
SQL code
/* Create a database */
CREATEDATABASE STUDENT_MANAGER;
USE STUDENT_MANAGER;
/***** Create a student table *****/
CREATETABLE STUDENT_TBL
(
STUDENT_ID VARCHAR (255) PRIMARYKEY,
STUDENT_NAME VARCHAR (10) NOTNULL,
STUDENT_SEX VARCHAR (10 ),
STUDENT_BIRTHDAY DATE,
CLASS_ID VARCHAR (255)
);
/* Insert student data */
INSERTINTO STUDENT_TBL (STUDENT_ID,
STUDENT_NAME,
STUDENT_SEX,
STUDENT_BIRTHDAY,
CLASS_ID)
VALUES (123456,
'Xxx ',
'Femal ',
'2017-08-01 ',
121546
)
Create the MySql. properties configuration file used to connect to mysql.
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 the MyBatis Environment
The order is casual. The current order is because you can modify as few files as possible.
1.2.3.1 create object class: StudentEntity
Java code
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 create a data access interface
The dao interface corresponding to the Student Class: StudentMapper.
Java code
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 create an SQL ing statement File
StudentMapper. xml
Xml Code
</p></li><li><p> SELECT * from STUDENT_TBL ST</p></li><li><p> WHERE ST.STUDENT_ID = #{studentID}</p></li><li><p>
</p></li><li><p> SELECT * from STUDENT_TBL</p></li><li><p>
1.2.3.4 create a mapper configuration file for MyBatis
Create the MyBatis profile: mybatis-config.xml in src/main/resource.
Xml Code
1.2.3.5 modify the Spring configuration file
It mainly adds the bean of the SqlSession production factory class: SqlSessionFactoryBean (in the mybatis. spring package ). You must specify the configuration file location and dataSource.
Bean corresponding to the data access interface. Created through MapperFactoryBean. You need to execute the full name of the interface class and reference the SqlSession factory bean.
Xml Code
You can also use annotations instead of defining mapper beans:
Add StudentMapper to Annotation
Java code
@ Repository
@ Transactional
Publicinterface StudentMapper {
}
The corresponding scan needs to be added to the dispatcher-servlet.xml:
Xml Code
1.2.4 test StudentMapper
Use SpringMVC for testing, create a TestController, configure tomcat, and access the index. do page for testing:
Java code
@ Controller
Publicclass TestController {
@ Autowired
Private StudentMapper studentMapper;
@ RequestMapping (value = "index. do ")
Publicvoid indexPage (){
StudentEntity entity = studentMapper. getStudent ("10000013 ");
System. out. println ("name:" + entity. getStudentName ());
}
}
Test with Junit:
Java code
Test with Junit:
Java code
@ 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 ());
}
}
}