Mybatis environment and mybatis environment Configuration

Source: Internet
Author: User

Mybatis environment and mybatis environment Configuration

Step 1: Download the jar package and import it

1. mysql driver package

2. mybatis environment package

Step 2: Create a MYSQL database

Because this is used for testing, only the test-usreinfo data table is created.

Step 3: Create the mybatis-cfg.xml In the src folder (mybatis global profile)

<?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="mybatis-mysql"><environment id="mybatis-mysql"><transactionManager type="jdbc"></transactionManager><dataSource type="POOLED"><property name="driver" value="com.mysql.jdbc.Driver" /><property name="url" value="jdbc:mysql://127.0.0.1:3306/test" /><property name="username" value="root" /><property name="password" value="" /></dataSource></environment></environments></configuration>

Step 4: view the jar package in the src File

1. com. shengmu. model: Data model package

2. com. shengmu. util: Processing Toolkit

3. com. shengmu. DAO: database operation package

4. com. shengmu. test: test package

Step 5: create User. class in com. shengmu. model: Data model package

 1 package com.shengmu.model; 2  3 public class User { 4      5     private int id; 6     private String username; 7     private String userpaw; 8     public int getId() { 9         return id;10     }11     public void setId(int id) {12         this.id = id;13     }14     public String getUsername() {15         return username;16     }17     public void setUsername(String username) {18         this.username = username;19     }20     public String getUserpaw() {21         return userpaw;22     }23     public void setUserpaw(String userpaw) {24         this.userpaw = userpaw;25     }26     27     @Override28     public String toString() {29         return "User [id=" + id + ", username=" + username + ", userpaw=" + userpaw + "]";30     }31     32 33 }

Step 6: Create usermapper. xml: data operation configuration file, such as select and insert. Put this file with User. class.

<?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.shengmu.model.usermapper" >    <select id="SelectUserByUsername"  parameterType="int" resultType="com.shengmu.model.User" >        select * from userinfo where id = #{id}    </select></mappe

Step 7: add the configuration of usermapper. xml in the mybatis-cfg.xml file.

 

<?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="mybatis-mysql">        <environment id="mybatis-mysql">            <transactionManager type="jdbc"></transactionManager>            <dataSource type="POOLED">                <property name="driver" value="com.mysql.jdbc.Driver" />                <property name="url" value="jdbc:mysql://127.0.0.1:3306/test" />                <property name="username" value="root" />                <property name="password" value="" />            </dataSource>        </environment>    </environments>    <mappers>        <mapper resource="com/shengmu/model/usermapper.xml" />    </mappers></configuration>

Where: <mappers> <mapper resource = "com/shengmu/model/usermapper. xml"/> </mappers> is usermapper. xml configured in the mybatis-cfg.xml.

Step 8: add the test class TestSelect. class to com. shengmu. test.

package com.shengmu.test;import java.io.InputStream;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import com.shengmu.model.User;public class TestSelect {    public static void main(String[] args) {        String resource = "mybatis-cfg.xml";        try {            InputStream inputStream = TestSelect.class.getClassLoader().getResourceAsStream(resource);            SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(inputStream);            SqlSession session = sessionFactory.openSession();            String statment = "com.shengmu.model.usermapper.SelectUserByUsername";            User user = session.selectOne(statment,1);                        session.commit();            System.out.println(user.getUsername());            session.close();        } catch (Exception e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}

Run as -- java application.

 

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.