1.创建java项目,并导入相关开发包
2. 创建表
create database mydb;
use mydb;
create table user (
id int primary key auto_increment,
name varchar(255),
age int
);
insert into user values (null,'aaa',19),(null,'bbb',29),(null,'ccc',39),(null,'ddd',22),(null,'eee',33);
3.创建bean
package cn.wy.domain;
public class User {
private int id;
private String name;
private int age;
public User() {
}
public User(int id, String name, int age) {
this.id = id;
this.name = name;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
4.编写配置文件sqlMapconfig.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>
<!-- 配置数据源 -->
<!--通过default可以切换当前使用的数据库-->
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/mydb?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="root" />
</dataSource>
</environment>
<environment id="oracle">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="oracle.jdbc.driver.OracleDriver" />
<property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:XE" />
<property name="username" value="ht1602" />
<property name="password" value="htdb" />
</dataSource>
</environment>
</environments>
<!-- 配置映射文件 -->
<mappers>
<mapper resource="UserMapper.xml" />
</mappers>
</configuration>
5.编写映射文件,编写sql
namespace:命名空间,需要全世界唯一,一般采用公司名称反写
id:保证当前项目唯一
userMapper.xml
<?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">
<!--namespace名称空间,需要唯一,一般以公司名称反写-->
<mapper namespace="cn.wy.mapper.UserMapper">
<select id="selec03" resultType="cn.wy.domain.User">
select * from user where id=#{id};
</select>
<insert id="insert1">
insert into user values (null ,#{name},#{age});
</insert>
<select id="selec01" resultType="cn.wy.domain.User">
select * from user;
</select>
<select id="selec02" resultType="cn.wy.domain.User">
select * from user where age between #{min} and #{max};
</select>
</mapper>
6.将映射文件配置到sqlMapperConfig.xml
具体请查看步骤4
<!-- 配置映射文件 -->
<mappers>
<mapper resource="UserMapper.xml" />
</mappers>
7.测试类中添加测试方法测试
@Test
public void test01() throws IOException {
InputStream inputStream = Resources.getResourceAsStream("slqMapConfig.xml");
SqlSessionFactory factory = new SqlSessionFactoryBuilder()
.build(inputStream);
SqlSession sqlSession = factory.openSession();
List<Object> list = sqlSession.selectList("cn.wy.mapper.UserMapper.selec01");
System.out.println("list:"+list);
sqlSession.close();
}
项目结构
测试结果
数据库数据