一、创建Maven项目并下载依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.7</version>
</dependency>
/*单元测试*/
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
二、创建表并插入数据
CREATE TABLE `user` (
id BIGINT AUTO_INCREMENT,
`name` VARCHAR(20),
`password` VARCHAR(20),
PRIMARY KEY(id)
)ENGINE=INNODB DEFAULT CHARSET=utf8
INSERT INTO `user` (`name`, `password`) VALUES ('a', '123');
INSERT INTO `user` (`name`, `password`) VALUES ('b', '123');
INSERT INTO `user` (`name`, `password`) VALUES ('c', '123');
三、创建User实体类
public class User {
private Long id;
private String name;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User(){
}
public User(Long id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
'}';
}
}
四、在mapper文件夹下创建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">
<mapper namespace="com.wsh.mapper.UserMapper">
<select id="selectUserById" resultType="com.wsh.pojo.User">
select * from user where id = #{id}
</select>
</mapper>
注:http://mybatis.org/dtd/mybatis-3-mapper.dtd用于语法约束
五、创建mybatis-config.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>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/test"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/UserMapper.xml"/>
</mappers>
</configuration>
六、编写JAVA程序
public void test() throws IOException {
//读取配置文件创建SqlSession工厂
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//利用SqlSession工厂创建SqlSession实例
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
User user = sqlSession.selectOne("com.wsh.mapper.UserMapper.selectUserById", 1);
System.out.println(user.toString());
}finally {
sqlSession.close();
}
}
七、总结
- SqlSession不是线程安全的,在Web开发中每次收到HTTP请求应创建一个SqlSession,并在使用完后关闭,为确保关闭操作能正常执行,应将关闭操作放入finally中。
- Mybatis推荐将SQL语句编写在xml文件中,能将JAVA代码和SQL语句分开,降低耦合度。
如采用注解方式写SQL(不推荐)
<?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.wsh.mapper.UserMapper">
</mapper>
public interface UserMapper {
@Select("select * from user where id = 2")
public User selectUserById(Long id);
}
@Test
public void test() throws IOException {
//读取配置文件创建SqlSession工厂
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//利用SqlSession工厂创建SqlSession实例
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
User user = mapper.selectUserById(1L);
System.out.println(user.toString());
}finally {
sqlSession.close();
}
}