Tutorial on installing and configuring mybatis in java + Spring + MySQL environment

1. Introduction and configuration of mybatis mybatis + Spring + MySQL

1.1mybatis introduction mybatis is a persistence layer framework that can customize SQL, stored procedures and advanced mapping. Mybatis eliminates most of the JDBC code, manually setting parameters and recovering result sets. Mybatis only uses simple XML and annotations to configure and map basic data types, map interfaces, and POJOs to database records. Compared with "one-stop" ORM solutions such as hibernate and Apache OJB, mybatis is a "semi-automatic" ORM implementation. Jar package to be used: mybatis-3.0 2. Jar (mybatis core package). Mybatis-spring-1.0.0.jar (combined with spring package). Download address: http://ibatis.apache.org/tools/ibator http://code.google.com/p/mybatis/ 1.2mybatis + Spring + MySQL simple configuration 1.2 1. Build spring environment (1) build Maven's Web project; (2) add spring framework and configuration files; (3) add required jar packages (spring framework, mybatis, mybatis spring, JUnit, etc.); (4) change the configuration files of web.xml and spring; (5) add a JSP page and corresponding controller; (6) test. Refer to: http://limingnihao.iteye.com/blog/830409 。 Build a spring MVC project using Maven of eclipse

1.2. 2 establish MySQL database and establish a student course selection management database. Table: student table, class table, teacher table, curriculum table, student course selection table. Logical relationship: each student has a class; Each class corresponds to a head teacher; Each teacher can only be the head teacher of one class; Use the following SQL to build the database. First create a student table and insert data (more than 2). For more SQL, please download the project source file in resource / SQL.

Create the configuration file used to connect to MySQL properties。

1.2. 3. The order of setting up mybatis environment is arbitrary. The current order is because the written files can be modified as little as possible.

1.2. 3.1 create entity class: studententity

1.2. 3.2 create the Dao interface corresponding to the student class of the data access interface: studentmapper.

1.2. 3.3 create SQL mapping statement file

SQL statement file of student class studentmapper XML resultmap tag: mapping between table fields and attributes. Select tag: query SQL.

1.2. 3.4 create the mapper configuration file of mybatis. Create the mybatis configuration file in Src / main / resource: mybatis config xml。 Typealiases tag: give the class an alias. com. manager. data. model. Studententity class, which can be replaced by studententity. Mappers tag: loads the SQL mapping statement file of the entity class in mybatis.

1.2. 3.5 modifying the configuration file of spring is mainly to add the bean of the production factory class of sqlsession: sqlsessionfactorybean (in the mybatis. Spring package). You need to specify the location of the configuration file and the datasource. The implementation bean corresponding to the data access interface is created through mapperfactorybean. You need to execute the full name of the interface class and the reference of the sqlsession factory bean.

You can also use annotation without defining the bean of mapper: add studentmapper to the annotation

The corresponding needs are in the dispatcher servlet Add scan to XML:

1.2. 4 test studentmapper uses spring MVC test, creates a testcontroller, configures tomcat, and accesses index Do page to test:

Test with JUnit:

2. The main configuration file of mybatis needs to specify the main configuration file of mybatis when defining sqlsessionfactory:

The sub tags of configuration in mybatis configuration file include:

2.1 properties property

Properties and Java Properties. Configure the resource specification of properties The path of properties, and then configure the name and value of property under the Properties tab The corresponding property value in the properties file.

2.2 setting this is an important step for mybatis to modify the details of the operation process. The table below describes these settings, their meanings and default values.

2.3 typealiases type aliases are short for Java types. It is simply associated with XML configuration, shorthand for verbose Java class names. For example:

With this configuration, "studententity" can be used anywhere instead of "com. Manager. Data. Model. Studententity". For normal Java types, there are many built-in type aliases. They are case insensitive. Due to the overloaded names, pay attention to the special handling of native types.

2.4 typehandlers type handles whether mybatis sets a parameter in the preprocessing statement or takes a value from the result set, the type processor is used to convert the obtained value into Java type in an appropriate way. The following table describes the default processor type.

Add the typehandler tag to typehandlers in the configuration file.

2.5 objectfactory object factory each time mybatis creates a new instance for the result object, objectfactory will be used. The default objectfactory is no different from using the constructor of the target class to create an instance. If there are mapped parameters, the constructor with parameters may also be used. If you override the default operation of objectfactory, you can inherit org apache. ibatis. reflection. factory. Defaultobjectfactory create your own. The objectfactory interface is simple. It contains two creation methods, one is to handle the default constructor, and the other is to handle the constructor with parameters. Finally, the setproperties method can be used to configure the objectfactory. After initializing your objectfactory instance, the properties defined in the objectfactory element body will be passed to the setproperties method.

Add the objectfactory tag to the configuration file

2.6 plugins plug-in

Mybatis allows you to intercept calls executed by mapped statements at a certain point. By default, mybatis allows plug-ins to intercept method calls:

The details of the methods in these classes can be found by looking at the signature of each method, and their source code is available in the mybatis distribution package. You should understand the behavior of your override method, assuming you do more than monitor calls. If you try to modify or overwrite a given method, you may break the core of mybatis. This is a low-level class and method, and plug-ins should be used with caution. Using plug-ins is a very simple power they provide. Simply implement the interceptor interface to determine the specified signature you want to intercept.

2.7 environments mybatis can configure multiple environments. This can help you map SQL to multiple databases, etc.

2.8 mappers mapper here is a statement that tells mybatis where to find mapped SQL. You can use the resource reference in the classpath, or use characters to enter the exact URL reference. For example:

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>