当前位置: 首页 > >

MyBatis 入门(一)--建立数据库连接及基本操作

发布时间:

一、 数据库建表

CREATE TABLE `user` (
`userId` int(11) NOT NULL AUTO_INCREMENT,
`UserName` varchar(45) NOT NULL,
`UserEmail` varchar(200) NOT NULL,
PRIMARY KEY (`userId`),
UNIQUE KEY `userId_UNIQUE` (`userId`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8;

二、maven配置



org.mybatis
mybatis
3.2.6



junit
junit
4.12
test



c3p0
c3p0
0.9.1.2



mysql
mysql-connector-java
6.0.2


三、mybasit 配置

    jdbc.properties 配置 数据连接

driver=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf8&useSSL=true&serverTimezone=UTC
username=mybatis
password=mybatis
maxActive= 50

    mybatis-config.xml


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




















四、建立User的POJO

package com.elements.user.model;

public class User {

public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getUserEmail() {
return UserEmail;
}
public void setUserEmail(String userEmail) {
UserEmail = userEmail;
}
private int userId;
private String UserName;
private String UserEmail;


@Override
public String toString() {
return "User [userId=" + userId + ", UserName=" + UserName
+ ", UserEmail=" + UserEmail + "]";
}


}

五、 UserMapper配置文件
UserMapper 配置文件


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















insert into mybatis.user (UserName, UserEmail) values (
#{UserName}, #{UserEmail}
)



update mybatis.user set UserName=#{UserName},
UserEmail=#{UserEmail}
where userId= #{userId}



delete mybatis.user where userId= #{userId}



将配置文件加入mybatis-config.xml的 mappers中






六、 建立测试类

public class TestUser {

@Test
public void TestSelectAll() throws IOException {
//
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);

SqlSession session = sqlSessionFactory.openSession();
try {

List userlist = session
.selectList("com.elements.user.mapper.getAllUser");

System.out.print(userlist);

}catch(Exception e){
System.out.print(e);
}
finally {
session.close();
}

}

@Test
public void TestSelect() throws IOException {
//
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);

SqlSession session = sqlSessionFactory.openSession();
try {
User user = (User) session.selectOne(
"com.elements.user.mapper.getUserById", "1");

System.out.print(user);

} finally {
session.close();
}

}

@Test
public void TestInsert() throws IOException {

String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);

SqlSession session = sqlSessionFactory.openSession();
try {
User user = new User();
user.setUserName("abc");
user.setUserEmail("aaaaa");

session.update("com.elements.user.mapper.insert", user);

session.commit();

} finally {
session.close();
}

}

@Test
public void TestUpdate() throws IOException {

String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder()
.build(inputStream);

SqlSession session = sqlSessionFactory.openSession();
try {
User user = new User();
user.setUserId(7);
user.setUserName("aaaaa");
user.setUserEmail("aaaaa");

session.update("com.elements.user.mapper.update", user);

session.commit();

} finally {
session.close();
}

}

}


项目代码下载:http://pan.baidu.com/s/1kUGD61D



友情链接: