Mybatis入门案例(一)

[size=medium][color=black]一、需求分析
对用户表进行增删改查操作
1、根据用户ID查询用户信息
2、根据用户名称模糊查询用户列表
3、添加用户[/color][/size]
[size=medium][color=cyan]二、工程搭建
环境准备:
Jdk:1.7
Ide:eclipse neon
Mybatis:3.2.7
数据库:MySQL 5X
Mybatis的核心包和依赖包:
asm-3.3.1.jar
cglib-2.2.2.jar
commons-logging-1.1.1.jar
javassist-3.17.1-GA.jar
log4j-1.2.17.jar
log4j-api-2.0-rc1.jar
log4j-core-2.0-rc1.jar
mybatis-3.2.7.jar
slf4j-api-1.7.5.jar
slf4j-log4j12-1.7.5.jar
MySQl的驱动包: mysql-connector-java-5.1.7-bin.jar[/color][/size]
[size=medium][color=darkblue]三、数据库脚本
create database mybatis
use mybatis
create table user(
id int primary key auto_increment,
username varchar(50),
sex varchar(10),
address varchar(100)
)
insert into user(username,sex,address) values(?,?,?)[/color][/size]
[size=large][color=red]四、代码实现[/color][/size]
[size=medium][color=green] <一>、创建pojo类
public class User {
private int id;
private String username;// 用户姓名
private String sex;// 性别
private String address;// 地址
}[/color][/size]
[size=medium][color=darkred] <二>、创建全局配置文件:在config目录下,创建SqlMapConfig.xml文件

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">










value="com.mysql.jdbc.Driver" />
value="jdbc:mysql://localhost:3306/mybatis" />









[/color][/size]
[size=medium][color=blue] <三>、需求开发
1、映射文件:在config目录下,创建User.xml

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">








select LAST_INSERT_ID()

insert into user(username,sex,address) values(#
{username},#{sex},#{address})

[/color][/size]
[size=medium][color=brown]<四>、测试代码
public class UserTest {

@Test
public void findUserByIdTest() throws Exception{
//读取配置文件
//全局配置文件
InputStream inputStream =
Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory
SqlSessionFactory sessionFactory = new
SqlSessionFactoryBuilder().build(inputStream);
//SqlSession
SqlSession openSession = sessionFactory.openSession();
User user = openSession.selectOne("test.selectUserById", 10);
System.out.println(user);
openSession.close();
}
@Test
public void findUserByUsernameTest() throws Exception{
//读取配置文件
//全局配置文件
InputStream inputStream =
Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory
SqlSessionFactory sessionFactory = new
SqlSessionFactoryBuilder().build(inputStream);
//SqlSession
SqlSession openSession = sessionFactory.openSession();
List list =
openSession.selectList("test.selectUserByUsername", "张");
System.out.println(list);
openSession.close();
}
@Test
public void insertUserTest() throws Exception{
//读取配置文件
//全局配置文件
InputStream inputStream =
Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory
SqlSessionFactory sessionFactory = new
SqlSessionFactoryBuilder().build(inputStream);
//SqlSession
SqlSession openSession = sessionFactory.openSession();
User user=new User();
user.setUsername("tom");
user.setSex("男");
user.setAddress("贵阳");
int i = openSession.insert("test.insertUser", user);
System.out.println(i);
openSession.commit();
openSession.close();
}
}[/color][/size]

你可能感兴趣的:(mybatis)