项目
mybatis对应依赖
<!-- MySql -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<!-- Mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.4.5</version>
</dependency>
<!--日志包-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
bean包类
package com.qst.bean;
public class User {
private int id;
private String username;
private String password;
public User() {
}
public User(String userName, String password) {
this.username = userName;
this.password = password;
}
public User(int id, String userName, String password) {
this.id = id;
this.username = userName;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUserName() {
return username;
}
public void setUserName(String userName) {
this.username = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
dao层类
com\qst\dao\UserMapper.java
package com.qst.dao;
import com.qst.bean.User;
import java.util.List;
public interface UserMapper {
public User findByName(String username);
public void saveUser(User user);
public void updateUser(User user);
public List<User> listUser();
public void deleteUser(String username);
}
jdbc连接封装类
package com.qst;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import java.io.InputStream;
//Session工具类
public class MySessionUtils {
private static SqlSessionFactory sessionFactory;
//static 静态代码,在类加载的时候执行一次,且只执行一次
static{
// 》1 创建SqlSessionFactoryBuilder对象
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
// 》2 创建SqlSessionFactory对象 com\qst\config\mybatis_config.xml
InputStream inputStream = MySessionUtils.class.getClassLoader().getResourceAsStream("com/qst/config/mybatis_config.xml");
sessionFactory = sqlSessionFactoryBuilder.build(inputStream);//加载核心配置文件 参1 输入流
// 》3 加载SqlMapConfig.xml配置文件
}
public static SqlSession getSession() {
// 》4 创建SqlSession对象
SqlSession sqlSession = sessionFactory.openSession();
return sqlSession;
}
}
数据库连接 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/mydb?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/qst/dao/UserMapper.xml"/>
</mappers>
</configuration>
SQL语句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.qst.dao.UserMapper">
<select id="findByName" parameterType="string" resultType="com.qst.bean.User">
select * from user where username = #{username};
/*ResultSet ResultSetMataData 列名--User 属性名 setUsername*/
</select>
<insert id="saveUser" parameterType="com.qst.bean.User" useGeneratedKeys="true" keyProperty="id">
insert into user(username,password) values(#{username},#{password});
</insert>
<select id="listUser" resultType="com.qst.bean.User">
select * from user ;
</select>
<update id="updateUser" parameterType="com.qst.bean.User" >
update user set password=#{password} where username=#{username};
</update>
<delete id="deleteUser" parameterType="string">
delete from user where username=#{username};
</delete>
</mapper>