执行原理
入门程序编写
1.pom.xml 文件
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.11</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.2.8</version>
</dependency>
</dependencies>
2.实体类和dao类
//接口类
public interface IuserDao {
List<User> findAll();
}
//实体类
public class User implements Serializable {
private String id;
private String user_name;
private String password;
private String name;
private Integer age;
private Integer sex;
private String created;
private Date birthdday;
private String updated
get and set 方法
}
3.配置主配置文件
<?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>
<!-- 环境,可以配置多个,default:指定采用哪个环境 -->
<environments default="test">
<!-- id:唯一标识 -->
<environment id="test">
<!-- 事务管理器,JDBC类型的事务管理器 -->
<transactionManager type="JDBC" />
<!-- 数据源,池类型的数据源 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1:3306/ssmdemo" />
<property name="username" value="root" />
<property name="password" value="12345678" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/dao/Iuser.xml"/> //这里要注意扫描的包是下划线
</mappers>
</configuration>
4.配置映射文件
<?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:命名空间,随便写,一般保证命名空间唯一 -->
<!--针对Usedao接口的配置-->
<!--通过mapper 将接口查到的数据,映射到实体类上去-->
<!--注意包的结构和代码的包结构保持一致,应该是方便映射,具体原因我不知道-->
<mapper namespace="com.dao.IuserDao">
<!-- statement,内容:sql语句。id:唯一标识,随便写,在同一个命名空间下保持唯一
resultType:sql语句查询结果集的封装类型,tb_user即为数据库中的表
-->
<!--//ID是接口里面的方法名字,不能出错-->
<!--//resultType 是映射到实体类,-->
<select id="findAll" resultType="com.domain.User">
select * from tb_user
</select>
</mapper>
5.数据库连接
创建数据库
CREATE DATABASE ssmdemo;
创建表
DROP TABLE IF EXISTS tb_user;
CREATE TABLE tb_user (
id char(32) NOT NULL,
user_name varchar(32) DEFAULT NULL,
password varchar(32) DEFAULT NULL,
name varchar(32) DEFAULT NULL,
age int(10) DEFAULT NULL,
sex int(2) DEFAULT NULL,
birthday date DEFAULT NULL,
created datetime DEFAULT NULL,
updated datetime DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
//插入的数据
INSERT INTO ssmdemo.tb_user ( id,user_name, password, name, age, sex, birthday, created, updated) VALUES ( '123','静静','123','yangtao','12',1, sysdate(), sysdate(),sysdate());
INSERT INTO ssmdemo.tb_user ( id,user_name, password, name, age, sex, birthday, created, updated) VALUES ( '456','秋秋','123','qiuqiu','12',2, sysdate(),sysdate(), sysdate());
6.测试类
package com.domain;
import com.dao.IuserDao;
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;
/**
* * @description: TODO
* * @param ${tags}
* * @return ${return_type}
* * @throws
* * @author yangtao
* * @date $date$ $time$
*
*/
public class MybatisTest {
public static void main(String[] args) throws Exception {
// 指定全局配置文件
String resource = "mybatis-config.xml";
// 读取配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
// 构建sqlSessionFactory,用于加载配置信息
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取sqlSession,会话处理数据库的方法
IuserDao iuserDao=sqlSession.getMapper(IuserDao.class);
//查询数据
List<User> user= iuserDao.findAll();
for (User u:user
) {
System.out.println(u.toString());
}
sqlSession.close();
}
}