Simple use of mybatis

Source: Internet
Author: User
Need to use the package :( here is only one version, other Baidu) mysql-connector-java-5.1.6-binmybatis-3.2.2 first look at the project directory: configuration file mybatisconfig. xml :? Xmlversion1.0encodingUTF-8 ?! DOCTYPEconfigurationPUBLIC-mybatis.orgDTDConfig3.0ENhttp:

Need to use the package :( here is only one of the versions, other Baidu) mysql-connector-java-5.1.6-bin mybatis-3.2.2 first look at the project directory: configuration file mybatisconfig. xml :? Xml version = 1.0 encoding = UTF-8 ?! DOCTYPE configurationPUBLIC-// mybatis.org//DTD Config 3.0 // ENhttp :/

Package to be used: (here is only one version, other Baidu)

Mysql-connector-java-5.1.6-bin

Mybatis-3.2.2

First look at the project directory:

Configuration File mybatisconfig. xml:

 
 
  
      
  
  
   
    
    
     
     
     
     
    
   
  
  
  
   
  
 
Entity class User. java:
package com.miquan.mybatis.bean;public class User {private int id;private String userName;private String password;public User(int id, String userName, String password) {super();this.id = id;this.userName = userName;this.password = password;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUserName() {return userName;}public void setUserName(String userName) {this.userName = userName;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}}
User. xml:
 
 
 
  
 
IUserOperation. java:
Package com. miquan. mybatis. inter; import java. util. list; import org. apache. ibatis. annotations. delete; import org. apache. ibatis. annotations. insert; import org. apache. ibatis. annotations. select; import org. apache. ibatis. annotations. update; import com. miquan. mybatis. bean. user; public interface IUserOperation {@ Select ("select * from User where id = # {id}") public User selectById (int id ); @ Select ("select * from User where userName = # {userName}") public List
 
  
SelectUsers (String userName); @ Insert ("insert into User values (null, # {userName}, # {password})") public boolean addUser (User user ); @ Delete ("delete from User where id = # {id}") public boolean delUser (int id ); @ Update ("update User" + "set userName = # {userName}," + "password = # {password}" + "where id = # {id }") public boolean updateUser (User user);} finally tested: Test. java:
 
Package com. miquan. mybatis. test; import java. io. inputStream; import java. util. list; import org. apache. ibatis. io. resources; import org. apache. ibatis. session. sqlSession; import org. apache. ibatis. session. sqlSessionFactory; import org. apache. ibatis. session. sqlSessionFactoryBuilder; import com. miquan. mybatis. bean. user; import com. miquan. mybatis. inter. IUserOperation; public class Test {static SqlSession session; public static void main (String [] args) {try {// obtain sessionInputStream is = Resources. getResourceAsStream ("mybatisconfig. xml "); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder (). build (is); session = sqlSessionFactory. openSession (); // XML configuration usage // User user User = session. selectOne ("com. miquan. mybatis. bean. userDB. selectById ", 1); // System. out. println (user. toString (); // interface call method // Note: After the insert, delete, and update methods are called, commit is required to change the data IUserOperation userOperation = session. getMapper (IUserOperation. class); // query // User user = userOperation. selectById (1); // System. out. println (user. toString (); // search for List // List
 
  
Users = userOperation. selectUsers ("miquan"); // System. out. println (users. size (); // Add // User user = new User (); // user. setUserName ("zhiquan"); // user. setPassword ("999"); // userOperation. addUser (user); // session. commit (); // Delete // userOperation. delUser (7); // session. commit (); // Change User user = new User (8, "qiantu", "shaxppp"); userOperation. updateUser (user); session. commit ();} catch (Exception e) {e. printStackTrace ();} Finally {try {session. close ();} catch (Exception e) {e. printStackTrace () ;}}} simple addition, query, modification, and so on.
 

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.