MyBatis Quick Start, mybatis
1. Background of MyBatis
MyBatis is an excellent persistent layer framework that supports common SQL queries, stored procedures, and advanced mappings. MyBatis eliminates the manual settings of almost all JDBC code and parameters and retrieves the result set. MyBatis uses simple XML or annotations for configuration and original ing. It maps interfaces and Java POJOs (Plain Old Java Objects, a common Java object) into records in the database.
2. MyBatis Development Environment build 2.1 prepare development environment
1. Create a New javaweb project MyBatis_Study in eclipse
2. Add the corresponding jar package
Mybatis-3.1.1.jar
Mysql-connector-java-5.1.7-bin.jar
(Note: You can search and download the jar package on the website http://www.manyjar.com/showJarHot)
3. Drag the jar package to the lib directory of the created Project.
4. Use SQLyog to create the database mybatis
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('codeRose',24);INSERT INTO users(NAME,age) VALUES('codeRose',24);
Select All SQL scripts to create databases and users tables:
2.2 Use MyBatis to query database data
1. Create the MyBatis configuration file conf. xml (under the src directory)
Conf. xml:
<? 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"/> <! -- Configure database connection information --> <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 =" "/> </dataSource> </environment> </environments> </configuration>
2. Define the corresponding object class User. java in the users table
User. java
Package codeRose. dao; public class User {// the attributes of the object class correspond to the private int id, private String name, private int age, and public int getId () {return id ;} public void setId (int id) {this. id = id;} public String getName () {return name;} public void setName (String name) {this. name = name;} public int getAge () {return age;} public void setAge (int age) {this. age = age ;}@ Override public String toString () {return "User [id =" + id + ", name =" + name + ", age = "+ age +"] ";}}