MyBatis初探

首先下载MyBatis jar包, 可以去MyBatis中文官网下载

项目中导入MyBatis jar包和JDBC jar包(此处用的MySQL)

新建conf.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/yourDatabase"/>
                <property name="username" value="root"/>
                <property name="password" value="yourPassword"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="下面配置文件的路径, 包名全称/???.xml"/>   
    </mappers>
</configuration>

  配置好后写个Student类测试:

 1 public class Student {
 2     int id;
 3     String name;
 4     int age;
 5     String sex;
 6 
 7     public int getId() {
 8         return id;
 9     }
10 
11     public void setId(int id) {
12         this.id = id;
13     }
14 
15     public String getName() {
16         return name;
17     }
18 
19     public void setName(String name) {
20         this.name = name;
21     }
22 
23     public int getAge() {
24         return age;
25     }
26 
27     public void setAge(int age) {
28         this.age = age;
29     }
30 
31     public String getSex() {
32         return sex;
33     }
34 
35     public void setSex(String sex) {
36         this.sex = sex;
37     }
38 
39     @Override
40     public String toString() {
41         return "Student{" +
42                 "id=" + id +
43                 ", name='" + name + '\'' +
44                 ", age=" + age +
45                 ", sex='" + sex + '\'' +
46                 '}';
47     }
48 }
View Code

带着toString()一会print方便

同时在数据库中创建student表,并插入几条数据:

 1 CREATE TABLE student
 2 (
 3     id int PRIMARY KEY,
 4     name VARCHAR(20),
 5     age INT,
 6     sex VARCHAR(4)
 7 )
 8 insert into student values(1, '张三', 20, '');
 9 insert into student values(2, '李四', 21, '');
10 insert into student values(3, '王五', 22, '');
11 insert into student values(4, '马六', 23, '');
View Code

配置Mapper.xml文件(上方Mapper resource写此文件)

<?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="定位到此的标识符">
    <!-- id为定位到语句的唯一标识符, parameterType为输入参数类型, 只能有一个, 可以用自定义类,returnType-为返回值类型, 也只能有一个, 注意,这两个如果是自定义类需要包名+类名 ->
    <select id="getOneStudent" parameterType="int" resultType="Student">
        select * from student where id=#{id}
    </select>
    <select id="getAllStudents" resultType="Student">
        select * from student
    </select>
</mapper>

用Mapper方式, 定义接口:

1 import java.util.List;
2 
3 public interface StudentMapper {
4     public Student getOneStudent(int id);
5     public List<Student> getAllStudents();
6 }
View Code

 

下面编写测试类:

 1 import org.apache.ibatis.io.Resources;
 2 import org.apache.ibatis.session.SqlSession;
 3 import org.apache.ibatis.session.SqlSessionFactory;
 4 import org.apache.ibatis.session.SqlSessionFactoryBuilder;
 5 
 6 import java.io.IOException;
 7 import java.io.InputStream;
 8 import java.util.List;
 9 
10 public class Test {
11     public static void main(String[] args) throws IOException {
12         String resource = "conf.xml";
13         InputStream inputStream = Resources.getResourceAsStream(resource);
14         SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
15         SqlSession session = sqlSessionFactory.openSession();
16         StudentMapper studentMapper = session.getMapper(StudentMapper.class);
17         Student s = studentMapper.getOneStudent(1);
18         List<Student> arrayStudents = studentMapper.getAllStudents();
19         System.out.println(s);
20         System.out.println(arrayStudents);
21     }
View Code

 

测试成功:

 

转载于:https://www.cnblogs.com/sqdtss/p/10098945.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值