一. 属性、别名和映射的配置
1. 属性配置
1.1. 属性文件配置
1.2. 加载和使用属性文件
2. 别名配置和使用
2.1. 别名配置
2.2. 别名使用
3. 映射配置
二. 属性、别名和映射的配置例子
1. 新建一个名为MybatisJdbcProperties的Java工程, 拷入相关jar包
2. 新建User.java
package com.lywgames.domain;
import java.util.Date;
public class User {
private Integer id;
private String name;
private String sex;
private Date birthday;
private String address;
public User() { }
public User(String name, String sex, Date birthday, String address) {
this.name = name;
this.sex = sex;
this.birthday = birthday;
this.address = address;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", sex=" + sex + ", birthday=" + birthday + ", address=" + address
+ "]";
}
}
3. 新建UserMapper.java接口
package com.lywgames.dao;
import java.util.List;
import com.lywgames.domain.User;
public interface UserMapper {
public List<User> selectUser(String name);
public int insertUser(User user);
public int updateUser(User user);
public int deleteUser(String name);
}
4. 在和UserMapper.java接口同一目录下新建UserMapper.xml映射文件
5. 在src目录下新建jdbc.properties数据库属性文件
6. 在src目录下新建SqlMapConfig.xml数据库配置文件
7. 新建Test.java
package com.lywgames;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
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 com.lywgames.dao.UserMapper;
import com.lywgames.domain.User;
public class Test {
public static void main(String[] args) {
SqlSession sqlSession = null;
try {
// 指定全局配置文件
String resource = "SqlMapConfig.xml";
// 读取配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
// 构建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获取sqlSession
sqlSession = sqlSessionFactory.openSession();
UserMapper userDao = sqlSession.getMapper(UserMapper.class);
// 插入操作
User insertUser = new User("李中", "男", new Date(System.currentTimeMillis()), "河南省郑州市");
int insertResult = userDao.insertUser(insertUser);
System.out.println("插入操作影响行数:" + insertResult);
// 查询操作
List<User> users = userDao.selectUser("李");
for (User user : users) {
System.out.println(user);
}
// 更新操作
User user = new User(4, "李苗", "女", null, null);
int updateResult = userDao.updateUser(user);
System.out.println("更新操作影响行数:" + updateResult);
// 删除操作
int deleteResult = userDao.deleteUser("英");
System.out.println("删除操作影响行数:" + deleteResult);
// 写操作, 需要提交事务。
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
if(sqlSession != null) {
// 释放资源
sqlSession.close();
}
}
}
}
8. 查看user表数据
9. 运行项目
10. 运行项目后查看user表数据