JavaEE architecture Chapter 7 Mybatis Framework  7.1 Introduction to Mybatis 7.2 Mybatis Quick Start 7.3 Basic CRUD operations 7.1 Introduction to Mybatis  MyBatis is an excellent persistence framework that supports common SQL queries, stored procedures, and advanced mappings.  MyBatis eliminates manual settings of almost all JDBC code and parameters and encapsulation of search results.  MyBatis can use simple XML or annotations to configure and map interfaces and Java POJOs (Plain Ordinary Java Objects) into records in the database. [return] 7.2 Mybatis Quick Start  Database: Create database estdb; Usetestdb; Create table users ( Idintauto_increment not null, Username varchar (20) not null, Password varchar (20), Primary key (id) ); Mybatis introductory example: Step 1:  Add mysql-connector-java dependencies;  Add Mybatis dependencies to pom.xml: <dependency> <groupId>mysql</groupId> <artifactId>mysql-connectorjava</artifactId> <version>5.1.45</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.4.5</version> </dependency> Step 2: Add Mybatis configuration file  New Mybatis-config.xml file under src/main/resources/mybatis: <? XML version = "1.0" encoding = "UTF-8"?> DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN", "http://mybatis.org/dtd/mybatis-3config.dtd"> <configuration> <environments default= "development"> <environment id="development"> <Transaction Manager type="JDBC"/> Configure Mysql <dataSource type= "POOLED"> database connection <property name="driver" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/testdb"/> <property name="username" value="root"/> <property name="password" value="dba"/> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/user Mapper.xml"/> Add Mapping Resource </mappers> File </configuration> UserMapper. XML is detailed on the following page Step 3: Create the entity class User  Create the User class under the src/main/java/entity package: User.ava Package entity; Public class User{ Int ID; String username; String password; // getter and setter ellipsis @Override Public String to String (){ Return "User [id =" + ID + ", username =" + username + ", password =" + password + "]"; } } Step 4: Create the SQL mapping file userMapper.  XML New userMapper.xml under src/main/resources/mapper: UserMapper.xml <? XML version = "1.0" encoding = "UTF-8"?> DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN", "http://mybatis.org/dtd/mybatis-3mapper.dtd"> <mapper namespace="mapper.user Mapper"> <select id="getUser" resultType="entity.User" parameterType="int"> Select * from users where id=#{id} </select> </mapper> Step 5: Main program  New Test.java under src/main/java: Import java.io.IOException; Import java.io.InputStream; Import org. apache. ibatis. io. Resources; Import org. apache. ibatis. session. SqlSession; Import org. apache. ibatis. session. SqlSessionFactory; Import org. apache. ibatis. session. SqlSessionFactory Builder; Importentity. User; Test.java Public class test{ Public static void main (String [] args) throws IOException{ String resource = mybatis / mybatis - config. xml; Read the Mybatis InputStreaminputStream = Resources. getResourceAsStream (resource); configuration file and create session SqlSessionFactorysqlSessionFactory = newSqlSessionFactoryBuilder (). build (inputStream); SqlSessionsqlSession = sqlSessionFactory. openSession (); String statement = mapper. userMapper. getUser; // Find the mapping SQL string Useruser = sqlSession. selectOne (statement, 1); SqlSession. close (); System. out. println (user); } } Executing a query returns a unique object [return] 7.3 Basic CRUD operations 1. Implementation based on XML 2. Implementation based on annotation 1. Implementation based on XML UserMapper.xml <? XML version = "1.0" encoding = "UTF-8"?> DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN", "http://mybatis.org/dtd/mybatis-3mapper.dtd"> <mapper namespace="mapper.user Mapper"> <! - Get a user object based on the ID query - >. <select id="getUser" resultType="entity.User" parameterType="int"> Select * from users where id = #{id} </select> <! - Create Users - > Create Users, Create Users, Create Users, Create Users, Create Users, Create Users, Create Users, Create Users, Create Users, Create Users <insert id="addUser" parameterType="entity.User"> Insert i

pptxDoc JavaEE - Chapter 7 - Mybatis Framework (4 Hours)

Professional Docs > Common > Other > Preview
22 Pages 0 Downloads 66 Views 3.0 Score
Tips: Current document can only be previewed at most page8,If the total number of pages in the document exceeds page 8,please download the document。
Uploaded by admin on 2019-10-16 00:30:20
Rate
You can enter 255 characters
What is my domain?( answer:www.45doc.com )
comments
  • No comments yet