Mybatis is an upgraded version of ibatis, a persistence layer framework based on Java. Mybatis is an excellent persistence layer framework that supports ordinary SQL queries, stored procedures and advanced mapping. Mybatis eliminates almost all manual settings of JDBC code and parameters and retrieval of result sets. MyBatis uses simple XML or annotations for configuration and original mapping, mapping interfaces and Java POJOs (Plain Old Java Objects, ordinary Java objects) into records in the database. Compared with "one-stop" ORM solutions such as Hibernate and Apache OJB, Mybatis is a "semi-automated" ORM implementation.
1. Create a maven project and build an infrastructure layer
##entity book entity bookid bookname bookprice dao IBookDAO Add book method addBook()IBookDAO.XML Sql statement to add book name and priceservice has the same method of adding book as dao layer addBook()serviceimpl Rewrite the addBook() method to inject a dao return dao.addBook()2. Dependencies of pom files
<!--Mybatis+Spring整合--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <!--mybatis依赖包--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.1</version> </dependency> <!--mysql依赖包--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <!--spring JDBC依赖--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.0.RELEASE</version> </dependency> <!--spring基础jar--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-beans --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.0.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.0.RELEASE</version> </dependency>
FAQ"
3. Configuration file under resources
1.jdbc.properties configuration file writes the url to which you are connecting to the database , driver, username, passwordBecause my Mysql has no password, I don’t need to write password 2.MyBatis-configuration.xml configuration fileYou only need to configure an alias 3. The next step is the configuration of the most important applicationContestSSM.xml fileWhen binding bookservice The ref attribute IBookDAO in the property is red and cannot be recognized by the tool. It is an object stored in memory and can only be obtained by running it. Therefore, the color display is abnormal and does not affect the compilation and operation.
4. Create a test class and use junit
to test success! Go to the database to query the added book title and price. At this point, a simple integration case is finished!The above is the detailed content of What are the steps to integrate spring with mybatis?. For more information, please follow other related articles on the PHP Chinese website!