1.使用注解开发
8.1 面向接口编程
8.2 注解开发的简单过程
1、注解在接口上实现
@Select("select * from m_user")
List<User> getUsers();
2、需要在核心配置文件绑定接口
<mappers>
<mapper class="edu.xalead.dao.UserMapper"/>
</mappers>
3.本质:反射机制实现的,底层:动态代理!
4.实现CRUD
- 我们可以在工具类创建的时候改成自动提交事务
public static SqlSession getSqlSession() {
// return sqlSessionFactory.openSession();
// 设置增删改查自动提交事务,true。
return sqlSessionFactory.openSession(true);
}
-
只需修改接口和测试类即可。
-
mybatis-config.xml中
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <!--引入外部配置文件--> <properties resource="db.properties"/> <typeAliases> <typeAlias type="edu.xalead.pojo.User" alias="user"/> <!--<package name="edu/xalead/pojo"/>--> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper class="edu.xalead.dao.UserMapper"/> </mappers> </configuration>
-
接口UserMapper代码:(注解实现)
package edu.xalead.dao; import edu.xalead.pojo.User; import org.apache.ibatis.annotations.*; import java.util.List; public interface UserMapper { //根据id查询用户 @Select("select * from m_user") List<User> getUsers(); @Select("select * from m_user where id = #{id}") User getUserById(@Param("id") int id); @Insert("insert into m_user values(#{id},#{name},#{pwd})") int addUser(User user); @Delete("delete from m_user where id = #{id}") void deleteUser(@Param("id") int id); @Update("update m_user set name=#{name},pwd=#{pwd} where id = #{id}") int updateUser(User user); }
-
测试代码:
package edu.xalead.dao; import edu.xalead.pojo.User; import edu.xalead.utils.MybatisUtils; import org.apache.ibatis.session.SqlSession; import org.junit.Test; import java.util.HashMap; import java.util.List; import java.util.Map; public class UserDaoTest { @Test public void test(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); List<User> userList = mapper.getUsers(); for (User user : userList) { System.out.println(user.toString()); } sqlSession.close(); } @Test public void test1(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); User user = mapper.getUserById(1); System.out.println(user); } @Test public void test2(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.addUser(new User(4,"哈哈","1231111")); } @Test public void test3(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.deleteUser(4); } @Test public void test4(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper mapper = sqlSession.getMapper(UserMapper.class); mapper.updateUser(new User(4,"嘻嘻","1231111")); } }
注意:一定要把接口注册到核心配置文件
关于:@Param()注解
- 基本类型的参数或者String类型,需要叫上
- 引用类型不需要加
- 如果只有一个基本类型的活,可以忽略,但建议大家都加上
- 我们在sql中引用的就是我们这里的@Param中舍弟那个的属性名
- #{} 是预编译的,防止sql注入,${} 没那么安全
扩充:IDEA中的Lombok
-
IDEA中:File→Settings→Plugins→搜索Lombok下载
-
引入Lombok的maven依赖。
<dependencies> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.8</version> </dependency> </dependencies>
-
@注解加载加载单独的一个变量上时,只生成这一个变量的方法
-
@Getter@Setter @FieldNameConstants @ToString @EqualsAndHashCode @AllArgsConstructor(所有参数构造) @NoArgsConstructor(无参构造) @Data(相当于添加了@Getter@Setter@ToString@NoArgsConstructor(无参构造))
-
pojo包数据库表的映射类-User.java代码:
package edu.xalead.pojo; import lombok.AllArgsConstructor; import lombok.Data; @Data @AllArgsConstructor @NoArgsConstructor public class User { private int id; private String name; private String pwd; }
欢迎访问我的个人博客:http://www.ayjup.cn