码迷,mamicode.com
首页 > 编程语言 > 详细

java数据访问(3):myBatis

时间:2015-12-11 23:47:55      阅读:262      评论:0      收藏:0      [点我收藏+]

标签:

   maven

        <!-- 导入Mysql数据库链接jar包 -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

        <!-- 导入mybatis 包 -->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.1.1</version>
        </dependency>

 

mybatis-config.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" />
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver" />
                <property name="url" value="jdbc:mysql://localhost:3306/test2" />
                <property name="username" value="root" />
                <property name="password" value="Sa1234$" />
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="UserDaoMapper.xml"/>
    </mappers>
</configuration>

 

创建实体类

package my.pojo;

public class User {
    private int userId;
    private String userName;
    private String password;
    private String comment;
    
    
    public int getUserId() {
        return userId;
    }
    
    public void setUserId(int userId) {
        this.userId = userId;
    }
  
    
    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;
    }
    
    
    public String getComment() {
        return comment;
    }
    public void setComment(String comment) {
        this.comment = comment;
    }
}

 

创建映射文件

<?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="my.dao.UserDao">


    <insert id="insert" parameterType="my.pojo.User">
        insert into
        user(userName,password,comment)
        values(#{userName},#{password},#{comment})
    </insert>

    <update id="update" parameterType="my.pojo.User">
        update user set
        userName=#{userName},password=#{password},comment=#{comment}
        where
        userName=#{userName}
    </update>

    <delete id="delete" parameterType="int">
        delete from user where
        userName=#{userName}
    </delete>

    <select id="findByUserName" parameterType="String" resultType="my.pojo.User">
        select * from user where userName=#{userName}
    </select>

    <select id="countAll" resultType="int">
        select count(*) c from user;
    </select>

    <select id="selectAll" resultType="my.pojo.User">
        select * from user order by
        userName asc
    </select>

    <select id="findById" parameterType="Int" resultType="my.pojo.User">
        select *
        from user where userId=#{userId}
    </select>

</mapper>

 

创建DAO

package my.dao;

import java.util.List;

import my.pojo.User;
 

public interface UserDao {
    
    public int insert(User user);
    
    public int update(User user);
    
    public int delete(String userName);
    
    public List<User> selectAll();
    
    public int countAll();
    
    public User findByUserName(String userName);
    
    public User findById(int userId);
}

 

 测试

package my.common;
import java.io.IOException;
import java.io.Reader;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

public class SessionHelper {

    static SqlSessionFactory factory;

    public static SqlSession getSession() throws IOException {

        if (factory == null) {
            Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
            factory = new SqlSessionFactoryBuilder().build(reader);
        }

        SqlSession session = factory.openSession();

        return session;

    }

}

 

package my.main;

import my.dao.UserDao;
import my.pojo.User;

import org.apache.ibatis.session.SqlSession;
import my.common.*;

public class App {
    public static void main(String[] args) {

        SqlSession session = null;
        try {

            session = SessionHelper.getSession();

            UserDao userDao = session.getMapper(UserDao.class);

            User user = userDao.findById(18);

            System.out.println(user.getUserName());

            session.commit();

        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
        } finally {
            if (session != null) {
                session.close();
            }

        }
    }
}

 

java数据访问(3):myBatis

标签:

原文地址:http://www.cnblogs.com/dehai/p/5033854.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!