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

JAVA mybitis入门学习

时间:2021-06-11 19:08:20      阅读:0      评论:0      收藏:0      [点我收藏+]

标签:filter   org   配置文件   stream   rac   oct   rgba   rom   jdb   

mybatis使用小案例

添加mybatis依赖

<!--mabatis依赖-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.1</version>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.1</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.9</version>
        </dependency>

打包配置文件  

        <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
                <filtering>false</filtering>
            </resource>    

在resources文件夹下新建mysql配置文件mysql.properties

driverClass=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/renren_fast
username=root
password=******

在resources文件夹下新建mybatis配置文件mybatis.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>
    <properties resource="mysql.properties"/>
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <!--数据库连接信息-->
                <dataSource type="POOLED">
                    <property name="driver" value="${driverClass}"/>
                    <property name="url" value="${url}"/>
                    <property name="username" value="${username}"/>
                    <property name="password" value="${password}"/>
                </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/dao/StudentDao.xml"/>
    </mappers>
</configuration>

初始化配置文件

public class MyBatisUtils {
    private static SqlSessionFactory sqlSessionFactory = null;
    static {
        String resource = "mybatis.xml";
        InputStream inputStream = null;
        try {
            inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static SqlSession getSqlSession(){
        SqlSession session = null;
        if (sqlSessionFactory != null){
            session = sqlSessionFactory.openSession();
        }
        return session;
    }
}

映射文件

<?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.dao.StudentDao">
    <select id="selectStudents" resultType="com.domain.Student">
        select user_id as id,username as name from tb_user
    </select>
    <select id="selectStudentOne" parameterType="int" resultType="com.domain.Student">
        select user_id as id,username as name from tb_user where user_id = #{stuId}
    </select>
</mapper>

StudentDao接口层

public interface StudentDao {
    public List<Student> selectStudents();
    public Student selectStudentOne(Integer stuId);
}

Student对象

public class Student {
    private int id;
    private String name;
    private String mobile;

    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 String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    @Override
    public String toString() {
        return "Student{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", mobile=‘" + mobile + ‘\‘‘ +
                ‘}‘;
    }

}

 

测试

public class myApp {

    public static void main(String[] args) throws IOException {

        SqlSession session = MyBatisUtils.getSqlSession();
        StudentDao dao = session.getMapper(StudentDao.class);
        List<Student> listStudent = dao.selectStudents();
        for (Student student : listStudent) {
            System.out.println("ID:" + student.getId() + ",NAME:" + student.getName());
        }
    }
}

 

数据库数据

技术图片

 

JAVA mybitis入门学习

标签:filter   org   配置文件   stream   rac   oct   rgba   rom   jdb   

原文地址:https://www.cnblogs.com/tianyun5115/p/14875482.html

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