- mybatis-config.xml
2.pojo
id, username, password, email, phone, question, answer, role, create_time, update_time
delete from mmall_user
where id = #{id,jdbcType=INTEGER}
insert into mmall_user (id, username, password,
email, phone, question,
answer, role, create_time,
update_time)
values (#{id,jdbcType=INTEGER}, #{username,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR}, #{phone,jdbcType=VARCHAR}, #{question,jdbcType=VARCHAR},
#{answer,jdbcType=VARCHAR}, #{role,jdbcType=INTEGER}, now(),
now())
insert into mmall_user
id,
username,
password,
email,
phone,
question,
answer,
role,
create_time,
update_time,
#{id,jdbcType=INTEGER},
#{username,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{email,jdbcType=VARCHAR},
#{phone,jdbcType=VARCHAR},
#{question,jdbcType=VARCHAR},
#{answer,jdbcType=VARCHAR},
#{role,jdbcType=INTEGER},
now(),
now(),
update mmall_user
username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
question = #{question,jdbcType=VARCHAR},
answer = #{answer,jdbcType=VARCHAR},
role = #{role,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = now(),
where id = #{id,jdbcType=INTEGER}
update mmall_user
set username = #{username,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
email = #{email,jdbcType=VARCHAR},
phone = #{phone,jdbcType=VARCHAR},
question = #{question,jdbcType=VARCHAR},
answer = #{answer,jdbcType=VARCHAR},
role = #{role,jdbcType=INTEGER},
create_time = #{createTime,jdbcType=TIMESTAMP},
update_time = now()
where id = #{id,jdbcType=INTEGER}
update mmall_user
SET password = #{passwordNew},update_time = now()
where username = #{username}
- 接口
package com.love.lee.dao;
import com.love.lee.pojo.User;
import org.apache.ibatis.annotations.Param;
public interface UserMapper {
int deleteByPrimaryKey(Integer id);
int insert(User record);
int insertSelective(User record);
User selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(User record);
int updateByPrimaryKey(User record);
int checkUsername(String username);
int checkEmail(String email);
User selectLogin(@Param("username") String username, @Param("password") String password);
String selectQuestionByUsername(String username);
int checkAnswer(@Param("username") String username, @Param("question") String question, @Param("answer") String answer);
int updatePasswordByUsername(@Param("username") String username, @Param("passwordNew") String passwordNew);
int checkPassword(@Param(value = "password") String password, @Param("userId") Integer userId);
int checkEmailByUserId(@Param(value = "email") String email, @Param(value = "userId") Integer userId);
}
4.调用
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession sqlSession = sqlSessionFactory.openSession();
try {
sqlSession = dao.getSqlSession();
UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
User user = userMapper.selectByPrimaryKey(1);
System.out.println(user.toString());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (sqlSession != null) {
sqlSession.close();
}
}