E:\java\MyBaits\mybatisdemo\src\com\bjsxt\pojo\Users.java
Log4j: Log For Java(Java 的日志) 是 Apache 提供的一个开源的 Java主流日志框架
Mybatis 中的别名配置它只和 XML 配置有关, 只用来减少类完全限定名的多余部分。注
意:别名都是大小写不敏感的。
使用 typeAlias 指定单个类的别名
<typeAliases>
<typeAlias type="com.bjsxt.pojo.Users" alias="u"/>
typeAliases>
<select id="selectUsersById" parameterType="int" resultType="u">
select * from users where userid = #{suibian}
select>
表示用u来代替com.bjsxt.pojo.Users
使用package指定某个包下所有类的默认别名
<typeAliases>
<package name="com.bjsxt.pojo"/>
typeAliases>
引入后的映射文件
<select id="selectUsersById" parameterType="int" resultType="users">
select * from users where userid = #{suibian}
select>
这段代码的主要意思呢就是给com.bjsxt.pojo这个包结构下所有的类都起一个别名这个别名就是这个类名的小写字母,以后就可以用小写字母来代替这个包结构了。
eg:
com.xxx.xxx.GoodsName就可以替换为goodsname
com.xxx.xxx.GoodsNumber就可以替换为goodsnumber
1.E:\java\MyBaits\mybatisdemo\src\com\bjsxt\mapper\UsersMapper.xml
<select id="selectUsersByNameAndSex" resultType="users">
select * from users where username = #{name} and usersex = #{sex}
select>
mapper>
//2.E:\java\MyBaits\mybatisdemo\src\com\bjsxt\dao\UsersDao.java
Map<Integer,Users> selectUsersByNameAndSex(String username, String usersex);
//3.E:\java\MyBaits\mybatisdemo\src\com\bjsxt\dao\impl\UsersDaoImpl.java(省略了UsersDao.java)
@Override
public Map<Integer, Users> selectUsersByNameAndSex(String username, String usersex) {
SqlSession sqlSession = MybatisUtils.getSqlSession();
Map<String,String> param = new HashMap<>();
param.put("name",username);//要和UserMapper.xml中username = #{name} and usersex = {sex}对应
param.put("sex",usersex);
Map<Integer, Users> users = sqlSession.selectMap("com.bjsxt.mapper.UserMapper.selectUsersByNameAndSex", param, "userid");
return users;
}
//4.E:\java\MyBaits\mybatisdemo\src\com\bjsxt\service\serviceimpl\UsersServiceImpl.java
@Override
public Map<Integer, Users> findUsersByNameAndSex(String username, String usersex) {
SqlSession sqlSession = MybatisUtils.getSqlSession();
Map<Integer, Users> integerUsersMap = null;
try {
UsersDaoImpl usersDao = new UsersDaoImpl();
integerUsersMap = usersDao.selectUsersByNameAndSex(username, usersex);
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
MybatisUtils.closeSqlSession();
}
return integerUsersMap;
}
//5.测试
@Test
public void findUsersByNameAndSex(){
UsersServiceImpl usersService = new UsersServiceImpl();
Map<Integer, Users> usersByNameAndSex = usersService.findUsersByNameAndSex("潘海宸", "男");
Set<Integer> integers = usersByNameAndSex.keySet();
for (Integer key:integers){
Users users = usersByNameAndSex.get(key);
System.out.println(key + "\t" + users.getUserid() + "\t" + users.getUsername() + "\t" + users.getUsersex());
}
}
在 MyBatis 中提供了另外一种 Dao 层的实现方式,既:Mapper 动态代理(或称为接口
绑定)的操作方式。这种方式下程序员只需要写 Dao 接口,不需要创建 Dao 的接口实现类,
Mybatis 会自动生成接口实现类的代理对象。在 Dao 层我们只要创建接口与映射配置文件即
可。这种方式可以大大简化 Dao 层的代码结构,是在开发中最常见的使用方式。
注意:引入mapper映射配置文件时mapper标签内有所不同,需要用package标签
<!--引入 Mapper 映射配置文件-->
<mappers>
<package name="com.bjsxt.mapper"/>
</mappers>
1.修改映射配置文件
<select id="selectUsersAll" resultType="users">
select * from users
select>
2.修改UserMapper接口添加抽象方法
public interface UsersMapper {
List<Users> selectUsersAll();
}
3.创建业务层接口
public interface UsersService {
List<Users> findUsersAll();
}
public List<Users> findUsersAll() {
List<Users> list = null;
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
//getMapper 方法的作用是根据给定的接口的 Class 对象,生成该接口的接口实现类的代理对象
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);//可以通过接口的名字定位到映射配置文件,所以要求映射配置文件里面的namespace属性的值要和使用接口的全名相同;
//定位到映射配置文件后,根据方法名定位到要2使用的标签,所以方法名要和id的值相同。这样就能执行标签里的Sql语句了
list = mapper.selectUsersAll();
} catch (Exception e) {
sqlSession.rollback();
e.printStackTrace();
} finally {
MybatisUtils.closeSqlSession();
}
return list;
}
5.测试
@Test
public void findUsersAll(){
UsersServiceImpl usersService = new UsersServiceImpl();
List<Users> usersAll = usersService.findUsersAll();
Iterator<Users> iterator = usersAll.iterator();
while (iterator.hasNext()){
Users next = iterator.next();
System.out.println(next.toString());
}
}
<select id="selectUsersById" parameterType="_int" resultType="users">
select * from users where userid = #{id}
select>
Users selectUsersById(int id);
Users findUsersById(int id);
@Override
public Users findUsersById(int id) {
SqlSession sqlSession = MybatisUtils.getSqlSession();
Users users = null;
try {
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
users = mapper.selectUsersById(id);
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
MybatisUtils.closeSqlSession();
}
return users;
}
@Test
public void findUsersById(){
UsersServiceImpl usersService = new UsersServiceImpl();
Users user = usersService.findUsersById(2);
System.out.println(user.toString());
}
注解中@param(“…”)中的参数代表的是Mapper映射文件中#{…}里面的值。
<select id="selectUsersAnnParam" resultType="users">
select * from users where username = #{name} and usersex= #{sex}
select>
List<Users> selectUsersAnnParam(@Param("name") String username,@Param("sex") String usersex);
List<Users> findUsersAnnParam(@Param("name") String username1, @Param("sex") String usersex);
@Override
public List<Users> findUsersAnnParam(String username1, String usersex) {
SqlSession sqlSession = MybatisUtils.getSqlSession();
List<Users> list = null;
try {
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
list = mapper.selectUsersAnnParam(username1, usersex);
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
MybatisUtils.closeSqlSession();
}
return list;
}
@Test
public void findUsersAnnParam(){
UsersServiceImpl usersService = new UsersServiceImpl();
List<Users> user = usersService.findUsersAnnParam("潘海宸", "男");
Iterator<Users> iterator = user.iterator();
while (iterator.hasNext()){
Users next = iterator.next();
System.out.println(next.toString());
}
在 Mapper 动态代理中也可以使用 POJO 作为传递参数的载体,在 SQL 语句中绑定参数
时使用 POJO 的属性名作为参数名即可。此方式推荐使用。
<select id="selectUsersPOJOParam" resultType="users">
select * from users where username = #{username} and usersex=#{usersex}
select>
List<Users> selectUsersPOJOParam(Users users);
List<Users> findUsersPOJOParam(Users users);
@Override
public List<Users> findUsersPOJOParam(Users users) {
SqlSession sqlSession = MybatisUtils.getSqlSession();
List<Users> list = null;
try {
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
list = mapper.selectUsersPOJOParam(users);
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
MybatisUtils.closeSqlSession();
}
return list;
}
@Test
public void selectUsersPOJOParam(){
UsersServiceImpl usersService = new UsersServiceImpl();
Users user = new Users();
user.setUsername("潘海宸");
user.setUsersex("男");
List<Users> usersPOJOParam = usersService.findUsersPOJOParam(user);
Iterator<Users> iterator = usersPOJOParam.iterator();
while (iterator.hasNext()){
Users next = iterator.next();
System.out.println(next.toString());
}
}
RowBounds 是 Mybatis 提供的一个专门处理分页的对象。在 RowBounds 对象中有两个成员变量:
<select id="selectUsersRowBounds" resultType="users">
select * from users
select>
List<Users> selectUsersRowBounds(RowBounds rowBounds);
List<Users> findUsersRowBounds();
@Override
public List<Users> findUsersRowBounds() {
SqlSession sqlSession = MybatisUtils.getSqlSession();
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
RowBounds rowBounds = new RowBounds(0, 20);
List<Users> list = mapper.selectUsersRowBounds(rowBounds);
return list;
}
@Test
public void findUsersRowBounds(){
UsersServiceImpl usersService = new UsersServiceImpl();
List<Users> usersRowBounds = usersService.findUsersRowBounds();
Iterator<Users> iterator = usersRowBounds.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next().toString());
}
}
在分页查询时,如果返回的结果较多,那么需要使用特定的 SQL 语句来实现分页处理。在 MySQL 数据库中我们可以使用 limit 实现分页。
<select id="selectUsersLimit" resultType="users">
select * from users limit #{offset},#{limit}
select>
List<Users> selectUsersLimit(@Param("offset") intoffset,@Param("limit") int limit);
List<Users> findUsersLimit(@Param("offset") int offset, @Param("limit") int limit);
@Override
public List<Users> findUsersLimit(int offset, int limit) {
SqlSession sqlSession = MybatisUtils.getSqlSession();
List<Users> list = null;
try {
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
list = mapper.selectUsersLimit(offset, limit);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
MybatisUtils.closeSqlSession();
}
return list;
}
public void findUsersLimit(){
UsersServiceImpl usersService = new UsersServiceImpl();
List<Users> users = usersService.findUsersLimit(0, 3);
Iterator<Users> iterator = users.iterator();
while (iterator.hasNext()){
Users next = iterator.next();
System.out.println(next.toString());
}
}
<insert id="insertUsers">
insert into users values (default , #{username}, #{usersex})
insert>
void insertUsers(Users users);
void InsertUsers(Users users);
@Override
public void InsertUsers(Users users) {
SqlSession sqlSession = MybatisUtils.getSqlSession();
try {
UsersMapper mapper = sqlSession.getMapper(UsersMapper.class);
mapper.insertUsers(users);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
MybatisUtils.closeSqlSession();
}
}
@Test
public void insertUsers(){
UsersServiceImpl usersService = new UsersServiceImpl();
Users user = new Users();
user.setUsername("沙雕");
user.setUsersex("男");
usersService.InsertUsers(user);
}
<insert id="insertUsersGetKey" useGeneratedKeys="true"keyProperty="userid">
insert into users values(default ,#{username},#{usersex})
insert>
全局配置:
<settings>
<setting name="useGeneratedKeys" value="true"/>
settings>
<insert id="insertUsersGetKey" keyProperty="userid">
insert into users values(default ,#{username},#{usersex})
insert>