SSM[mybaits] 搭建和入门

环境搭建

  1. 创建 maven 工程

  2. 创建实体类和dao接口

    [User.java]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    package com.gphper.domain;

    import java.util.Date;

    public class User {
    int id;
    String number;
    String name;
    String password;
    String gender;
    Date create_time;
    Date update_time;
    int sex;

    public int getId() {
    return id;
    }

    public void setId(int id) {
    this.id = id;
    }

    public String getNumber() {
    return number;
    }

    public void setNumber(String number) {
    this.number = number;
    }

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public String getPassword() {
    return password;
    }

    public void setPassword(String password) {
    this.password = password;
    }

    public String getGender() {
    return gender;
    }

    public void setGender(String gender) {
    this.gender = gender;
    }

    public Date getCreate_time() {
    return create_time;
    }

    public void setCreate_time(Date create_time) {
    this.create_time = create_time;
    }

    public Date getUpdate_time() {
    return update_time;
    }

    public void setUpdate_time(Date update_time) {
    this.update_time = update_time;
    }

    public int getSex() {
    return sex;
    }

    public void setSex(int sex) {
    this.sex = sex;
    }

    @Override
    public String toString() {
    return "User{" +
    "id=" + id +
    ", number='" + number + '\'' +
    ", name='" + name + '\'' +
    ", password='" + password + '\'' +
    ", gender='" + gender + '\'' +
    ", create_time=" + create_time +
    ", update_time=" + update_time +
    ", sex=" + sex +
    '}';
    }
    }

    [IUserDao]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    package com.gphper.dao;

    import com.gphper.domain.User;

    import java.util.List;

    public interface IUserDAo {
    List<User> findAll();
    }
  3. 创建Mybatis的主配置文件 SqlMapConfig.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    <?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"/>
    <!--配置数据源:创建Connection对象-->
    <dataSource type="POOLED">
    <!--driver:驱动内容-->
    <property name="driver" value="com.mysql.jdbc.Driver"/>
    <!--连接数据库的url-->
    <property name="url" value="jdbc:mysql://localhost:3306/book?useUnicode=true&amp;characterEncoding=utf-8"/>
    <!--用户名-->
    <property name="username" value="root"/>
    <!--密码-->
    <property name="password" value="123456"/>
    </dataSource>
    </environment>
    </environments>

    <mappers>
    <mapper resource="com/gphper/dao/IUserDao.xml"/>
    </mappers>
    </configuration>
  4. 创建映射配置文件 IUserDao.xml

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <?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.gphper.dao.IUserDAo">
    <!-- 配置查询所有 -->
    <select id="findAll" resultType="com.gphper.domain.User">
    select * from user
    </select>
    </mapper>
  5. 入门代码【xml版】

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
           package com.gphper;

    import com.gphper.dao.IUserDAo;
    import com.gphper.domain.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.InputStream;
    import java.util.List;

    public class MybatisTest {
    public static void main(String[] args) throws Exception {
    //读取配置文件
    InputStream in = Resources.getResourceAsStream("MysqlMapConf.xml");
    // 创建SqlSessionFactory 工厂
    SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
    SqlSessionFactory build = sqlSessionFactoryBuilder.build(in);
    // 使用工厂创建SqlSession 对象
    SqlSession sqlSession = build.openSession();
    // 使用session对象创建Dao接口的代理对象
    IUserDAo userDao = sqlSession.getMapper(IUserDAo.class);
    // 使用代理对象执行方法
    List<User> users = userDao.findAll();
    for(User user:users){
    System.out.println(user);
    }
    //释放资源
    sqlSession.close();
    in.close();
    }
    }
    ```
    * 注意事项
    > mybaits的映射配置文件位置必须和dao接口的的包结构相同
    > 配置文件的`mapper`标签`namespace`属性的取值必须是dao接口的全限定类名
    > 映射配置文件的操作配置id属性的值必须是dao接口的方法名

    6. 注解版
    IUserDao.xml
    ``` xml
    <mappers>
    <mapper class="com.gphper.dao.IUserDAo" />
    </mappers>
    [IUserDao]
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    package com.gphper.dao;

    import com.gphper.domain.User;

    import java.util.List;

    public interface IUserDAo {

    @Select("select * from user")
    List<User> findAll();
    }