根据用户ID查询用户信息
根据用户名查找用户列表
添加用户
package com.itzheng.mybatis.dao;
import java.util.List;
import com.itzheng.mybatis.pojo.User;
public interface UserDao {
// 根据用户ID查询用户信息
/*
* 用户信息持化接口
*/
User getUserById(Integer id);
// 根据用户名查找用户列表
List<User> getUserByUserName(String userName);
// 添加用户
void insertUser(User user);
}
public class UserDaoImpl implements UserDao {
@Override
public User getUserById(Integer id) {
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
User user = sqlSession.selectOne("user.getUserByid", id);
sqlSession.close();
return user;
}
@Override
public List<User> getUserByUserName(String userName) {
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
List<User> selectList = sqlSession.selectList("user.getUserByUserName", userName);
sqlSession.close();
return selectList;
}
@Override
public void insertUser(User user) {
SqlSession sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(true);// true自动提交
sqlSession.insert("user.insertUser", user);
sqlSession.close();
}
}
@Test
public void testGetUserById() {
UserDao userDao = new UserDaoImpl();
User user = userDao.getUserById(30);
System.out.println(user);
}
@Test
public void testGetUserByUserName() {
UserDao userDao = new UserDaoImpl();
List<User> userByUserName = userDao.getUserByUserName("张");
for (User user : userByUserName) {
System.out.println(user);
}
}
@Test
public void testInsertUser() {
UserDao userDao = new UserDaoImpl();
User user = new User();
user.setUsername("张飞1111");
user.setSex("2");
user.setBirthday(new Date());
user.setAddress("深圳黑马");
userDao.insertUser(user);
}
通过动态代理的方式有实现类但是不需要自己手动去写
package com.itzheng.mybatis.mapper;
import java.util.List;
import com.itzheng.mybatis.pojo.User;
public interface UserMapper {
// 根据用户ID查询用户信息
/*
* 用户信息持化接口
*/
User getUserByid(Integer id);
// 根据用户名查找用户列表
List<User> getUserByUserName(String userName);
// 添加用户
void insertUser(User user);
}
<mapper namespace="com.itzheng.mybatis.mapper.UserMapper">
<select id="getUserByid" parameterType="int"
resultType="com.itzheng.mybatis.pojo.User">
SELECT * FROM USER WHERE id = #{id1}
select>
<select id="getUserByUserName" parameterType="string"
resultType="com.itzheng.mybatis.pojo.User">
SELECT id,username,birthday,sex,address FROM `user` WHERE username LIKE
'%${value}%'
select>
<insert id="insertUser"
parameterType="com.itzheng.mybatis.pojo.User" useGeneratedKeys="true"
keyProperty="id">
INSERT INTO USER
(`username`,
`birthday`,
`sex`,
`address`)
VALUES (#{username},
#{birthday},
#{sex},
#{address});
insert>
mapper>
<mapper resource="mybatis/UserMapper.xml" />
@Test
public void testGetUserById() {
SqlSession openSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
// 获取接口的代理实现类
UserMapper userMapper = openSession.getMapper(UserMapper.class);
User user = userMapper.getUserByid(30);
System.out.println(user);
openSession.close();
}
@Test
public void testGetUserByUserName() {
SqlSession openSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
// 获取接口的代理实现类
UserMapper userMapper = openSession.getMapper(UserMapper.class);
List<User> list = userMapper.getUserByUserName("张");
for (User user : list) {
System.out.println(user);
}
openSession.close();
}
@Test
public void testInsertUser() {
SqlSession openSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession(true);
// 获取接口的代理实现类
UserMapper userMapper = openSession.getMapper(UserMapper.class);
User user = new User();
user.setUsername("张飞1111");
user.setSex("2");
user.setBirthday(new Date());
user.setAddress("深圳黑马");
userMapper.insertUser(user);
openSession.close();
}
<configuration>
<properties resource="jdbc.properties" >
<property name="jdbc.username" value="root"/>
<property name="jdbc.password" value="root"/>
properties>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
dataSource>
environment>
environments>
<mappers>
<mapper resource="mybatis/user.xml" />
<mapper resource="mybatis/UserMapper.xml" />
mappers>
configuration>
别名的作用将复杂的数据用其他的名称简单的代替
<typeAliases>
<package name="com.itzheng.mybatis.pojo"/>
typeAliases>
别名不区分大小写
<select id="getUserByUserName" parameterType="string"
resultType="user">
SELECT id,username,birthday,sex,address FROM `user` WHERE username LIKE
'%${value}%'
select>
<package name="com.itzheng.mybatis.pojo" />
1、接口文件必须与映射文件在同一个目录下
2、接口文件的名称必须与映射文件的名称一致
<package name="com.itzheng.mybatis.mapper"/>
1、接口文件必须与映射文件在同一个目录下
2、接口包文件的名称必须与映射文件的名称一致
<package name="com.itzheng.mybatis.mapper"/>