环境说明
什么是MyBatis
持久化
持久化是将数据从持久状态和瞬间状态相互转移的一种机制
持久层
什么是持久层?
为什么需要MyBatis
测试相关包junit_4.12.jar,org.hamcrest.core_1.3.0.jar
MyBatis依赖包mybatis-3.4.1.jar
mysql-connector-java-5.1.7-bin.jar
log4j.rootLogger=DEBUG,A1
log4j.logger.org.apache=DEBUG
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%c]-[%p] %m%n
<configuration>
<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://127.0.0.1:3306/mybatis" />
<property name="username" value="root" />
<property name="password" value="1234" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="UserDaoMapper.xml" />
mappers>
configuration>
<mapper namespace="UserDaoMapper">
<select id="queryUserById" resultType="com.atguigu.mybatis.pojo.User">
select * from tb_user where id = #{id}
select>
<select id="queryUserAll" resultType="com.atguigu.mybatis.pojo.User">
select * from tb_user
select>
<insert id="insertUser" parameterType="com.atguigu.mybatis.pojo.User">
INSERT INTO tb_user (
user_name,
password,
name,
age,
sex,
birthday,
created,
updated
)
VALUES
(
#{userName},
#{password},
#{name},
#{age},
#{sex},
#{birthday},
NOW(),
NOW()
);
insert>
<update id="updateUser" parameterType="com.atguigu.mybatis.pojo.User">
UPDATE tb_user
SET
user_name = #{userName},
password = #{password},
name = #{name},
age = #{age},
sex = #{sex},
birthday = #{birthday},
updated = NOW()
WHERE
(id = #{id});
update>
<delete id="deleteUserById" parameterType="java.lang.Long">
delete from tb_user where id=#{id}
delete>
mapper>
public class User {
private Long id;
private String userName;
private String password;
private String name;
private Integer age;
private Integer sex;
private Date birthday;
private Date created;
private Date updated;
}
public interface UserDao {
/**
* 根据id获取用户信息
* @param id
* @return
*/
public User queryUserById(long id);
/**
* 查询全部用户信息
* @return
*/
public List<User> queryUserAll();
/**
* 新增用户
* @param user
*/
public void insertUser(User user);
/**
* 更新用户信息
* @param user
*/
public void updateUser(User user);
/**
* 根据id删除用户信息
* @param id
*/
public void deleteUserById(Long id);
}
public class UserDaoImpl implements UserDao {
private SqlSession sqlSession;
public UserDaoImpl(SqlSession sqlSession) {
this.sqlSession = sqlSession;
}
@Override
public User queryUserById(long id) {
return this.sqlSession.selectOne("UserDaoMapper.queryUserById",id);
}
@Override
public List<User> queryUserAll() {
return this.sqlSession.selectList("UserDaoMapper.queryUserAll");
}
@Override
public void insertUser(User user) {
this.sqlSession.insert("UserDaoMapper.insertUser",user);
//提交
this.sqlSession.commit();
}
@Override
public void updateUser(User user) {
this.sqlSession.update("UserDaoMapper.updateUser", user);
this.sqlSession.commit();
}
@Override
public void deleteUserById(Long id) {
this.sqlSession.delete("UserDaoMapper.deleteUserById", id);
this.sqlSession.commit();
}
}
public class UserDaoTest {
private UserDao userDao;
@Before
public void setUp() throws Exception {
String resource = "mybatis-config.xml";
//读取配置文件
InputStream inputStream = Resources.getResourceAsStream(resource);
//构建sqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//获取sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
this.userDao = new UserDaoImpl(sqlSession);
}
@Test
public void queryUserById() {
System.out.println(userDao.queryUserById(1l));
}
@Test
public void queryUserAll() {
userDao.queryUserAll().forEach(System.out::println);
}
//仅展示前两个功能测试代码
}
参考第一篇博客
https://blog.csdn.net/Fang_1996/article/details/107122202