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>
<!-- 映入外部配置文件-->
<properties resource="db.properties"/>
<!-- 可以给实体类取别名-->
<typeAliases>
<!-- 指定一个包名,MyBatis会在包名下面搜索需要的Java Bean,比如:
扫描实体类的包,它的默认别名就为这个类的类名,首字母小写!-->
<!-- <package name="com.light.pojo"/>-->
<!-- 为具体一个类指定一个别名-->
<typeAlias type="com.light.pojo.User" alias="user"/>
</typeAliases>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="com/light/dao/UserMapper.xml"/>
</mappers>
</configuration>
db.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useSSL=false&useUnicode=true&characterEncoding=utf-8
username=root
password=123456
UserMapper.java
package com.light.dao;
import com.light.pojo.User;
import java.util.List;
import java.util.Map;
/**
* @author Light
* @Description
* @create 2022-04-27 14:08
*/
public interface UserMapper {
//查询全部用户
List<User> getUserList();
//根据id查询用户
User getUserById(int id);
//insert一个用户
int insertUser(User user);
//修改一个用户
int updateUser(User user);
//删除一个用户
int deleteUser(int id);
//万能的Map
int updateUser2(Map<String,Object> map);
}
UserMapper.xml
这里是将UserMaapper与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">
<!--namespacWe=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.light.dao.UserMapper">
<!-- id就是Dao/Mapper里面的方法名-->
<select id="getUserList" resultType="com.light.pojo.User">
select * from user
</select>
<select id="getUserById" parameterType="int" resultType="com.light.pojo.User">
select * from mybatis.user where id = #{id}
</select>
<insert id="insertUser" parameterType="com.light.pojo.User">
insert into mybatis.user(id, name, pwd) values(#{id}, #{name}, #{pwd})
</insert>
<update id="updateUser" parameterType="com.light.pojo.User">
update mybatis.user set name=#{name}, pwd=#{pwd} where id=#{id} ;
</update>
<delete id="deleteUser" parameterType="int">
delete from mybatis.user where id=#{id}
</delete>
<!-- 万能的Map-->
<update id="updateUser2" parameterType="map">
update mybatis.user set pwd=#{password} where id = #{panid};
</update>
</mapper>
User实体类
package com.light.pojo;
/**
* @author Light
* @Description
* @create 2022-04-27 14:06
*/
public class User {
private int id;
private String name;
private String pwd;
public User(int id, String name, String pwd) {
this.id = id;
this.name = name;
this.pwd = pwd;
}
public User() {
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", pwd='" + pwd + '\'' +
'}';
}
}
获取sqlsession
package com.light.utils;
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;
/**
* @author Light
* @Description
* @create 2022-04-27 13:56
*/
//SqlSessionFactory ----> sqlSession
public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static{
try {
//使用Mybatis第一步获取sqlSessionFactory对象
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} catch (IOException e) {
e.printStackTrace();
}
}
//既然有了 SqlSessionFactory,顾名思义,我们可以从中获得 SqlSession 的实例。
// SqlSession 提供了在数据库执行 SQL 命令所需的所有方法。你可以通过 SqlSession
// 实例来直接执行已映射的 SQL 语句。例如:
public static SqlSession getSqlSession(){
//如果 return sqlSessionFactory.openSession(true);则会自动提交事务,不填则为false(不会自动提交事务)
return sqlSessionFactory.openSession();
}
}