Mybaits课堂

1

public interface UserMapper {
public List getUserListByNullParam();
public List getUesrListByUserName(String userName);
public List getUserList(User user);
public List getUserListByMap(Map userMap);
public List getUserList();
public int add(User user);
public int modify(User user);

}

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">





























insert into smbms_user(userCode,userName,userPassword,gender,birthday,phone,address,userRole,createdBy,creationDate) values(#{userCode},#{userName},#{userPassword},#{gender},#{birthday},#{phone},#{address},#{userRole},#{createdBy},#{creationDate})



update smbms_user set userCode=#{userCode},userPassword=#{userPassword}
where id=#{id}

dao

 

2

//Serializable 序列化 网络传输
public class User implements Serializable {
private Integer id; //id
private String userCode; //用户编码
private String userName; //用户名称
private String userPassword; //用户密码
private Integer gender; //性别
private Date birthday; //出生日期
private String phone; //电话
private String address; //地址
private Integer userRole; //用户角色
private Integer createdBy; //创建者
private Date creationDate; //创建时间
private Integer modifyBy; //更新者
private Date modifyDate; //更新时间
private String userRoleName; //用户角色名称

 

public String getUserRoleName() {
return userRoleName;
}
public void setUserRoleName(String userRoleName) {
this.userRoleName = userRoleName;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUserCode() {
return userCode;
}
public void setUserCode(String userCode) {
this.userCode = userCode;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
public Integer getGender() {
return gender;
}
public void setGender(Integer gender) {
this.gender = gender;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getUserRole() {
return userRole;
}
public void setUserRole(Integer userRole) {
this.userRole = userRole;
}
public Integer getCreatedBy() {
return createdBy;
}
public void setCreatedBy(Integer createdBy) {
this.createdBy = createdBy;
}
public Date getCreationDate() {
return creationDate;
}
public void setCreationDate(Date creationDate) {
this.creationDate = creationDate;
}
public Integer getModifyBy() {
return modifyBy;
}
public void setModifyBy(Integer modifyBy) {
this.modifyBy = modifyBy;
}
public Date getModifyDate() {
return modifyDate;
}
public void setModifyDate(Date modifyDate) {
this.modifyDate = modifyDate;
}

@Override
public String toString() {
return "User{" +
"id=" + id +
", userCode='" + userCode + '\'' +
", userName='" + userName + '\'' +
", userPassword='" + userPassword + '\'' +
", gender=" + gender +
", birthday=" + birthday +
", phone='" + phone + '\'' +
", address='" + address + '\'' +
", userRole=" + userRole +
", createdBy=" + createdBy +
", creationDate=" + creationDate +
", modifyBy=" + modifyBy +
", modifyDate=" + modifyDate +
", userRoleName='" + userRoleName + '\'' +
'}';
}
}

pojo

 

3

public class MybatisUtils {
private static SqlSessionFactory sqlSessionFactory;
static {
String path = "mybatis-config.xml";
try {
InputStream is = Resources.getResourceAsStream(path);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
} catch (IOException e) {
e.printStackTrace();
}


}
public static SqlSession getSqlSession(){
//事务
// 默认true 关闭 自动提交
//false 开启
return sqlSessionFactory.openSession();
}
public static void closeSqlSession(SqlSession sqlSession){
if(null != sqlSession)
sqlSession.close();
}
}

util

 

4

public class TestUserMapper {
@Test
public void test01(){
//1.记载核心配置文件
String path = "mybatis-config.xml";
InputStream is = null;
try {
is = Resources.getResourceAsStream(path);
//2.mybatis的核心类
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
SqlSession sqlSession = sqlSessionFactory.openSession();
int result = sqlSession.selectOne("com.kgc.dao.UserMapper.count");
System.out.println(result);
} catch (IOException e) {
e.printStackTrace();
}

}
@Test
//使用工具类
public void test02(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
List userList = sqlSession.selectList("com.kgc.dao.UserMapper.userlist");
for (User user : userList) {
System.out.println(user);
}
}
@Test
//接口方式(以后全部用接口方式)
public void test03(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
List userList = sqlSession.getMapper(UserMapper.class).getUserListByNullParam();
for (User user : userList) {
System.out.println(user);
}

}
@Test
public void test04(){
String userName = "赵";
SqlSession sqlSession = MybatisUtils.getSqlSession();
List userList = sqlSession.getMapper(UserMapper.class).getUesrListByUserName(userName);
for (User user : userList) {
System.out.println(user);
}
}
@Test
//多条件查询
public void test05(){
User user = new User();
user.setUserName("赵");
user.setUserRole(2);
SqlSession sqlSession = MybatisUtils.getSqlSession();
List userList = sqlSession.getMapper(UserMapper.class).getUserList(user);
for (User user1 : userList) {
System.out.println(user1.getUserName());
}
}
@Test
//Map查询
public void test06(){
List userList = new ArrayList<>();
Map userMap = new HashMap<>();
SqlSession sqlSession = MybatisUtils.getSqlSession();
userMap.put("userName","赵");
userMap.put("userRole","2");
List userListByMap = sqlSession.getMapper(UserMapper.class).getUserListByMap(userMap);
for (User user : userListByMap) {
System.out.println(user.getUserName());
}


}
@Test
public void test07(){
User user = new User();
user.setUserName("赵");
user.setUserRole(2);
SqlSession sqlSession = MybatisUtils.getSqlSession();
List userList = sqlSession.getMapper(UserMapper.class).getUserList(user);
for (User user1 : userList) {
System.out.println(user1.getUserName()+"*******"+user1.getUserRoleName()+
"*******"+user1.getAddress());
}
}
@Test
public void testAdd(){
int count = 0;
SqlSession sqlSession = null;
User user = null;
try{
sqlSession = MybatisUtils.getSqlSession();
user = new User();
user.setUserCode("test001");
user.setUserName("测试001");
user.setAddress("测试地址");
user.setBirthday(new SimpleDateFormat("yyyy-MM-dd").parse("2019-10-21"));
user.setGender(1);
user.setUserPassword("123456");
user.setUserRole(2);
user.setCreatedBy(1);
user.setCreationDate(new Date());
count = sqlSession.getMapper(UserMapper.class).add(user);
//int i = 2/0;
sqlSession.commit();
}catch (Exception e){
e.printStackTrace();
sqlSession.rollback();
count = 0;

 

}finally {
MybatisUtils.closeSqlSession(sqlSession);
}
System.out.println(count);
}
@Test
public void testModify(){
int count = 0;
SqlSession sqlSession = null;
User user = null;
try{
sqlSession = MybatisUtils.getSqlSession();
user = new User();
user.setId(16);
user.setUserPassword("654321");
user.setUserCode("009");
count = sqlSession.getMapper(UserMapper.class).modify(user);
sqlSession.commit();
}catch (Exception e){
//sqlSession.rollback();
e.printStackTrace();
}finally {
MybatisUtils.closeSqlSession(sqlSession);
}
System.out.println(count);
}
}

test

 

5


PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
























































你可能感兴趣的:(Mybaits课堂)