1.先在mysql创建一个数据库再创建一个user表
create database mybatis;
CREATE TABLE user (
id INT ( 11 ) NOT NULL auto_increment,
username VARCHAR ( 32 ) NOT NULL COMMENT '用户名称' ,
birthday datetime default NULL COMMENT '生日' ,
sex char ( 1 ) default null comment '性别' ,
address varchar ( 256 ) default null comment '地址' ,
PRIMARY KEY ( `id`)
)
2.插入三条数据
insert into user ( username, birthday, sex, address) VALUES
( '张三' , '1995-1-24' , '男' , '上海市' ) ,
( '李四' , '1996-2-24' , '男' , '上海市' ) ,
( '王五' , '1997-10-24' , '男' , '上海市' )
3.新建一个maven工程,导入jar包
<?xml version="1.0" encoding="UTF-8"?>
< project xmlns = " http://maven.apache.org/POM/4.0.0"
xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance"
xsi: schemaLocation= " http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >
< modelVersion> 4.0.0</ modelVersion>
< groupId> com.oyzc</ groupId>
< artifactId> mybatis</ artifactId>
< version> 1.0-SNAPSHOT</ version>
< dependencies>
< dependency>
< groupId> org.mybatis</ groupId>
< artifactId> mybatis</ artifactId>
< version> 3.5.1</ version>
</ dependency>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.38</ version>
</ dependency>
< dependency>
< groupId> log4j</ groupId>
< artifactId> log4j</ artifactId>
< version> 1.2.17</ version>
</ dependency>
< dependency>
< groupId> junit</ groupId>
< artifactId> junit</ artifactId>
< version> 4.12</ version>
</ dependency>
</ dependencies>
</ project>
4.配置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>
< environments default = " mysql" >
< environment id = " mysql" >
< transactionManager type = " JDBC" > </ transactionManager>
< dataSource type = " POOLED" >
< property name = " driver" value = " com.mysql.jdbc.Driver" />
< property name = " url" value = " jdbc:mysql://localhost:3306/mybatis?serverTimezone=GMT& characterEncoding=utf-8& useSSL=false" />
< property name = " username" value = " root" />
< property name = " password" value = " 123456" />
</ dataSource>
</ environment>
</ environments>
< mappers>
< mapper resource = " dao/IUserDao.xml" > </ mapper>
</ mappers>
</ configuration>
5.配置映射文件
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
< mapper namespace = " dao.IUserDao" >
< select id = " findAll" >
select * from user
</ select>
</ mapper>
6.测试类
import dao. IUserDao;
import entity. 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 ( "mybatis.xml" ) ;
SqlSessionFactoryBuilder builder= new SqlSessionFactoryBuilder ( ) ;
SqlSessionFactory factory= builder. build ( in) ;
SqlSession session= factory. openSession ( ) ;
IUserDao userDao= session. getMapper ( IUserDao. class ) ;
List< User> users= userDao. findAll ( ) ;
for ( User user: users) {
System. out. println ( user) ;
}
session. close ( ) ;
in. close ( ) ;
}
}
注意事项
mybatis 的映射配置文件位置必须和dao接口的包结构相同 映射配置文件的mapper标签namespace属性的取值必须是dao接口的全限定类名 映射配置文件的操作配置(select),id属性的取值必须是dao接口的方法名 如果使用注解的话就不需要配置映射文件,只需要在dao接口方法上使用@Select(“select * from user”)指定sql语句 在mybatis.xml中mapper标签使用class属性指定dao接口的全限定类名