Mybatis学习记录(1)
一个简单应用的案列:
1.创建一个java工程
并把目录结构变成这样(只是为了和以后的maven的项目结构一致,养成一个规范)
2在com.mybatis目录下创建包lib,里面加入jar包,然后右键弹出一个目录,点add to libray
3在resource目录下添加mybatis的配置文件conf.xml
```java
<?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>
<!--开启驼峰命名-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true" />
</settings>
//配置数据源
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/zpark?characterEncoding=utf-8" />
<property name="username" value="root" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/mybatis/dao/UserMapper.xml" />
</mappers>
</configuration>
4.在com.mybatis_01.entity包中创建一个User实体类
```java
package com.mybatis.entity;
import java.util.Date;
public class User {
private Integer userId;
private String userName;
private String userPassword;
private String userEmail;
private Integer userSex;
private String userPhoto;
private Date userBirthday;
public User() {
}
public User(Integer userId, String userName, String userPassword, String userEmail, Integer userSex, String userPhoto, Date userBirthday) {
this.userId = userId;
this.userName = userName;
this.userPassword = userPassword;
this.userEmail = userEmail;
this.userSex = userSex;
this.userPhoto = userPhoto;
this.userBirthday = userBirthday;
}
@Override
public String toString() {
return "User{" +
"userId=" + userId +
", userName='" + userName + '\'' +
", userPassword='" + userPassword + '\'' +
", userEmail='" + userEmail + '\'' +
", userSex=" + userSex +
", userPhoto='" + userPhoto + '\'' +
", userBirthday=" + userBirthday +
'}';
}
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public Integer getUserSex() {
return userSex;
}
public void setUserSex(Integer userSex) {
this.userSex = userSex;
}
public String getUserPhoto() {
return userPhoto;
}
public void setUserPhoto(String userPhoto) {
this.userPhoto = userPhoto;
}
public Date getUserBirthday() {
return userBirthday;
}
public void setUserBirthday(Date userBirthday) {
this.userBirthday = userBirthday;
}
}
5在com.mybatis_01.dao包下创建一个UserMapper接口,往里面写一个方法根据id查询用户:
ackage com.mybatis.dao;
import com.mybatis.entity.User;
public interface UserMapper {
User selectById(Integer userId);//根据id查询用户
}
6 在com.mybatis_01.dao包下创建一个UserMapper.xml文件来写UserMapper中方法要执行的sql语句。
```java
<?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">
<mapper namespace="com.mybatis.dao.UserMapper">
<select id="selectById" parameterType="java.lang.Integer"
resultType="com.mybatis.entity.User">
select * from user where user_id=#{userId}
</select>
</mapper>
7 测试:在com.mybatis_01.test包下创建一个Test类来测试:
package com.mybatis;
import com.mybatis.dao.UserMapper;
import com.mybatis.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.IOException;
import java.io.InputStream;
public class Test {
public static void main(String[] args) {
//1 设置加载配置文件
String resource = "conf.xml";
InputStream input = Test.class.getClassLoader().getResourceAsStream(resource);
//2 创建SqlSessionFactory
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(input);
/* InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);*/
//3 开启session
SqlSession sqlSession= factory.openSession();
//从session中拿到mapper
try {
//4 使用getMapper方法获取接口,就可以调用里面的方法
UserMapper mapper=sqlSession.getMapper(UserMapper.class);
User user= mapper.selectById(1);
System.out.println(user);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
} finally {
//5关闭session
sqlSession.close();
}
}
}