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/mybatis?serverTimezone=Asia/Shanghai"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</dataSource>
</environment>
</environments>
<!--引入映射文件-->
<mappers>
<mapper resource="mappers/UserMapper.xml"/>
</mappers>
</configuration>
User.java
package com.example.mybatis;
public class User {
private Integer id;
private String username;
private String password;
public User() {
}
public User(Integer id, String username, String password) {
this.id = id;
this.username = username;
this.password = password;
}
public Integer getId() {
return id;
}
public void setId(Integer 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 + '\'' +
'}';
}
}
UserMapper.java
package com.example.mybatis;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper {
/**
* 根据id查询用户
* @param id
* @return
*/
User queryById(@Param("id") Integer id);
/**
* 插入用户
* @param user
* @return
*/
int insertUser(User user);
/**
* 查询所有用户
* @return
*/
List<User> queryAll();
/**
* 根据id删除
* @param id
* @return
*/
int deleteById(@Param("id") Integer id);
/**
* 更新数据
* @param user
* @return
*/
int update(User user);
}
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.example.mybatis.UserMapper">
<select id="queryById" resultType="com.example.mybatis.User">
select * from t_user where id = #{id}
</select>
<insert id="insertUser">
insert into t_user values(#{id},#{username},#{password})
</insert>
<select id="queryAll" resultType="com.example.mybatis.User">
select * from t_user
</select>
<delete id="deleteById">
delete from t_user where id = #{id}
</delete>
<update id="update">
update t_user set username = #{username} ,password = #{password} where id = #{id}
</update>
</mapper>
test.java
package com.example.mybatis;
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 org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class test {
@Test
public void demo() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory build = sqlSessionFactoryBuilder.build(is);
SqlSession sqlSession = build.openSession(true);
UserMapper mapper = sqlSession.getMapper(UserMapper.class);
// User user = mapper.queryById(1);
// System.out.println(user);
// int i = mapper.insertUser(new User(null, "张三", "123"));
// System.out.println(i);
mapper.update(new User(3,"张三","123456"));
List<User> users = mapper.queryAll();
System.out.println(users);
// int i = mapper.deleteById(2);
// System.out.println(i);
}
}