1. Introduction and configuration of MyBatis + Spring + MySql

Source: Internet
Author: User
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

  1. /* Create a database */

  2. CREATEDATABASE STUDENT_MANAGER;

  3. USE STUDENT_MANAGER;

  4. /***** Create a student table *****/

  5. CREATETABLE STUDENT_TBL

  6. (

  7. STUDENT_ID VARCHAR (255) PRIMARYKEY,

  8. STUDENT_NAME VARCHAR (10) NOTNULL,

  9. STUDENT_SEX VARCHAR (10 ),

  10. STUDENT_BIRTHDAY DATE,

  11. CLASS_ID VARCHAR (255)

  12. );

  13. /* Insert student data */

  14. INSERTINTO STUDENT_TBL (STUDENT_ID,

  15. STUDENT_NAME,

  16. STUDENT_SEX,

  17. STUDENT_BIRTHDAY,

  18. CLASS_ID)

  19. VALUES (123456,

  20. 'Xxx ',

  21. 'Femal ',

  22. '2017-08-01 ',

  23. 121546

  24. )



Create the MySql. properties configuration file used to connect to mysql.

Mysql. properties code

  1. Jdbc. driverClassName = com. mysql. jdbc. Driver

  2. 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

  1. Publicclass StudentEntity implements Serializable {

  2. Privatestaticfinallong serialVersionUID = 3096154202413606831L;

  3. Private ClassEntity classEntity;

  4. Private Date studentBirthday;

  5. Private String studentID;

  6. Private String studentName;

  7. Private String studentSex;

  8. Public ClassEntity getClassEntity (){

  9. Return classEntity;

  10. }

  11. Public Date getStudentBirthday (){

  12. Return studentBirthday;

  13. }

  14. Public String getStudentID (){

  15. Return studentID;

  16. }

  17. Public String getStudentName (){

  18. Return studentName;

  19. }

  20. Public String getStudentSex (){

  21. Return studentSex;

  22. }

  23. Publicvoid setClassEntity (ClassEntity classEntity ){

  24. This. classEntity = classEntity;

  25. }

  26. Publicvoid setStudentBirthday (Date studentBirthday ){

  27. This. studentBirthday = studentBirthday;

  28. }

  29. Publicvoid setStudentID (String studentID ){

  30. This. studentID = studentID;

  31. }

  32. Publicvoid setStudentName (String studentName ){

  33. This. studentName = studentName;

  34. }

  35. Publicvoid setStudentSex (String studentSex ){

  36. This. studentSex = studentSex;

  37. }

  38. }




1.2.3.2 create a data access interface

The dao interface corresponding to the Student Class: StudentMapper.

Java code

  1. Publicinterface StudentMapper {

  2. Public StudentEntity getStudent (String studentID );

  3. Public StudentEntity getStudentAndClass (String studentID );

  4. Public List GetStudentAll ();

  5. Publicvoid insertStudent (StudentEntity entity );

  6. Publicvoid deleteStudent (StudentEntity entity );

  7. Publicvoid updateStudent (StudentEntity entity );

  8. }



1.2.3.3 create an SQL ing statement File


StudentMapper. xml

Xml Code

  1. </p></li><li><p> SELECT * from STUDENT_TBL ST</p></li><li><p> WHERE ST.STUDENT_ID = #{studentID}</p></li><li><p>

  2. </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

  1. @ Repository

  2. @ Transactional

  3. Publicinterface StudentMapper {

  4. }


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

  1. @ Controller

  2. Publicclass TestController {

  3. @ Autowired

  4. Private StudentMapper studentMapper;

  5. @ RequestMapping (value = "index. do ")

  6. Publicvoid indexPage (){

  7. StudentEntity entity = studentMapper. getStudent ("10000013 ");

  8. System. out. println ("name:" + entity. getStudentName ());

  9. }

  10. }



Test with Junit:

Java code

  1. Test with Junit:

  2. Java code

  3. @ RunWith (value = SpringJUnit4ClassRunner. class)

  4. @ ContextConfiguration (value = "test-servlet.xml ")

  5. Publicclass StudentMapperTest {

  6. @ Autowired

  7. Private ClassMapper classMapper;

  8. @ Autowired

  9. Private StudentMapper studentMapper;

  10. @ Transactional

  11. Publicvoid getStudentTest (){

  12. StudentEntity entity = studentMapper. getStudent ("10000013 ");

  13. System. out. println ("" + entity. getStudentID () + entity. getStudentName ());

  14. List StudentList = studentMapper. getStudentAll ();

  15. For (StudentEntity entityTemp: studentList ){

  16. System. out. println (entityTemp. getStudentName ());

  17. }

  18. }

  19. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.