Shang Silicon Valley-mybatis Quick Start,-mybatis Quick Start

Source: Internet
Author: User

Shang Silicon Valley-mybatis Quick Start,-mybatis Quick Start
1. Introduction to Mybatis (ibatis)

MyBatis is an excellent persistent layer framework that supports common SQL queries, stored procedures, and advanced mappings. MyBatis eliminates the manual setup of almost all JDBC code and parameters and retrieval encapsulation of result sets. MyBatis can use simple XML or annotations for configuration and original ing. It maps interfaces and Java POJO (Plain Old Java Objects, common Java Objects) into records in the database.

JDBC ---> dbutils (automatic encapsulation) ---> MyBatis ---> Hibernate




2. mybatis Quick Start

Write the first test example based on mybaits:

Project Structure:


2.1. Add a jar package

[Mybatis]

Mybatis-3.1.1.jar

MYSQL driver package]
Mysql-connector-java-5.1.7-bin.jar

 

2.2. database creation + tables

Create database mybatis;

Use mybatis;

Create table users (id int primary key AUTO_INCREMENT, name varchar (20), age INT );

Insert into users (NAME, age) VALUES ('Tom ', 12 );

Insert into users (NAME, age) VALUES ('jack', 11 );


2.3. Add the configuration file conf. xml of Mybatis.
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><configuration><environments default="development"><environment id="development"><transactionManager type="JDBC"/><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://localhost:3306/mybatis" /><property name="username" value="root" /><property name="password" value="123456" /></dataSource></environment></environments></configuration>

2.4. Define the object class corresponding to the table

Public class User {

Private int id;

Private String name;

Private int age;

// Get, set Method

}


2.5. Define the SQL ing file userMapper. xml for operating the users table
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="com.atguigu.mybatis.bean.userMapper"><select id="getUser" parameterType="int" resultType="com.atguigu.mybatis.bean.User">select * from users where id=#{id}</select></mapper>

2.6. Register userMapper. xml in the conf. xml file
<mappers><mapper resource="com/atguigu/mybatis/bean/userMapper.xml"/></mappers>
2.7. Compile the test code: Execute the defined select statement 
Package com. atguigu. mybatis. test1; import java. io. IOException; import java. io. inputStream; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import org. apache. ibatis. session. sqlSessionFactoryBuilder; import com. atguigu. mybatis. bean. user;/*** mybatis Quick Start */public class Test1 {public static void main (String [] args) throws IOException {String resource = "conf. xml "; // use the class loader to load the configuration file InputStream of mybatis = Test1.class. getClassLoader (). getResourceAsStream (resource); // construct the sqlSession factory SqlSessionFactory factory = new SqlSessionFactoryBuilder (). build (is); // create sqlSessionSqlSession session = factory that can execute SQL IN THE ing file. openSession (); // String statement = "com. atguigu. mybatis. bean. userMapper. getUser "; // run the query to return the sqlUser user = session of a unique user object. selectOne (statement, 2); System. out. println (user );}}


Related Article

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.