美文网首页
mybatis 环境配置

mybatis 环境配置

作者: wubo | 来源:发表于2016-09-01 11:38 被阅读62次

    一. 创建用户表

    DROP TABLE IF EXISTS `user`;
    CREATE TABLE `user`(
    `id` INT(10) NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(100) NOT NULL,
    `email` VARCHAR(100) NOT NULL,
    `age` INT(10) NOT NULL,
    PRIMARY KEY(`id`)
    )ENGINE=InnoDB DEFAULT CHARSET=utf8;
    

    二. 新建工程

    1.添加依赖库

    <dependency>
          <groupId>org.mybatis</groupId>
          <artifactId>mybatis</artifactId>
          <version>3.4.1</version>
    </dependency>
    <!-- mysql -->
    <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.30</version>
    </dependency>
    

    2.设置mybatis 配置文件:Configuration.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>
        <typeAliases>
            <typeAlias alias="User" type="com.souke.mybatis.model.User"/>
        </typeAliases>
    
        <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=""/>
                </dataSource>
            </environment>
        </environments>
    
        <mappers>
            <mapper resource="mybatisMapper/User.xml"/>
        </mappers>
    </configuration>
    

    3. 新建User对象

    public class User {    
    private String id;    
    private String name;    
    private String email;   
    private int age;
    ...
    }
    

    4. 建立这个User 的映射文件 User.xml

    <?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.souke.mybatis.model.UserMapper">
        <select id="selectUserByID" parameterType="int" resultType="User">
            select * from user where id = #{id}
        </select>
        <insert id="addUser" parameterType="User">
            INSERT INTO user(name, email, age) VALUES ( #{name}, #{email}, #{age})
        </insert>
    
    </mapper>
    

    5. 工程结构

    6.新建测试类

    package com.souke.mybatis.sample;
    
    import com.souke.mybatis.model.User;
    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 java.io.IOException;
    import java.io.Reader;
    
    /**
     * Created by Bob on 2016/9/1.
     */
    public class Test {
        static String namespace = "com.souke.mybatis.model.UserMapper.";
        private static SqlSessionFactory sqlSessionFactory;
    
        static {
            try {
                Reader reader = Resources.getResourceAsReader("config/Configuration.xml");
    
    
                sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    
        public static SqlSessionFactory getSession() {
            return sqlSessionFactory;
        }
    
        public static void main(String[] args) {
            insert();
            query();
        }
    
        private static void insert() {
            SqlSession session = sqlSessionFactory.openSession();
    
            User user = new User();
            user.setEmail("email1@qq.com");
            user.setName("name1");
            user.setAge(1);
    
            int result = session.insert(namespace + "addUser", user);
            session.commit();
            System.out.println(result);
            session.close();
        }
    
        private static void query() {
            SqlSession session = sqlSessionFactory.openSession();
            User user = session.selectOne(namespace + "selectUserByID", 1);
            System.out.println(user.getName());
            session.close();
        }
    }
    

    result

    1
    name1
    

    运行程序,正确返回结果。环境搭建配置成功。

    相关文章

      网友评论

          本文标题:mybatis 环境配置

          本文链接:https://www.haomeiwen.com/subject/vrxcettx.html