myBatis在dao层开发
定义接口
实现接口
类继承接口后不想实现抽象方法那么就要定义为抽象类
没有方法传入用resultType
2,代理开发方式 动态代理 jdk的动态代理 cglib动态代理
1.需要自己写接口 Mapper接口
2.MyBatis通过动态代理的方式生成代理对象
代理对象的作用:和传统方式自己写的dao的实现类的作用一样的
@Test
public void testAdd(){
/填class类型 获取userMapper接口的实现类的对象,在底层使用动态代理的方式生成
UserMapper userMapper = session.getMapper(UserMapper.class);
//调用相应的方法
User user = new User();
user.setUsername("张三");
user.setPassword("123456");
int e = userMapper.add(user);
System.out.println(user);
//提交事务
session.commit();
}
UserMapper.xml中
id要和接口的方法名相同 参数也要相同:写在parameterType
返回:写在resultType 要和接口 包装类型一致
UserMapper.xml中的知识点
1,sQL语句的抽取
2-根据传入的内容,动态的组织sql语句 if标签 有1=1 想去除的话用where标签
3,添加时获取主键的值sout(user)时要在UserMapper的添加里
<selectKey keyColumn="id" keyProperty="id" resultType="long" order="AFTER">
SELECT last_insert_id()
</selectKey>
UserMapper.xml
<mapper namespace="com.hpe.mapper.UserMapper">
<!--sQL语句的抽取-->
<sql id="sql1">
select * from user
</sql>
<!--c查询1:根据username进行查询-->
<!--c查询1:根据username和password进行查询-->
<!--根据传入的内容,动态的组织sql语句 if标签-->
<select id="findByCondition" parameterType="user" resultType="user">
select * from user
<!--<if test="id!=null">-->
<!--and id=#{id}-->
<!--</if>-->
<!--<if test="username!=null">-->
<!--and username=#{username}-->
<!--</if>-->
<!--<if test="password!=null">-->
<!--and password=#{password}-->
<!--</if>-->
<include refid="sql1" />
<where>
<if test="id!=null">
and id=#{id}
</if>
<if test="username!=null">
and username=#{username}
</if>
<if test="password!=null">
and password=#{password}
</if>
</where>
</select>
<!--集合 foreach-->
<select id="findByIds" resultType="user">
<include refid="sql1" />
<where><!--遍历那多个id在接口中是List<User> findByIds(List<Long> ids);
表示集合:
id为1.2.4.5.的User
id in (1,3,5) 与下面的对应
-->
<foreach collection="list" open="id in (" close=")" separator="," item="id">
#{id}
</foreach>
</where>
</select>
<!--查询所有-->
<select id="findAll" resultType="user" >
select * from user;
</select>
<!--根据id查询-->
<select id="findById" parameterType="long" resultType="user">
SELECT * FROM user WHERE id=#{id}
</select>
<!--添加-->
<insert id="add" parameterType="user" >
<selectKey keyColumn="id" keyProperty="id" resultType="long" order="AFTER">
SELECT last_insert_id()
</selectKey>
INSERT INTO user(username,password) VALUES (#{username},#{password})
</insert>
<!--删除-->
<delete id="del" parameterType="long">
DELETE FROM user WHERE id=#{id}
</delete>
<!--修改-->
<update id="chg" parameterType="user">
UPDATE user SET username=#{username}, password=#{password} WHERE id=#{id}
</update>
<!--根据用户名和密码查询-->
<select id="fig" resultType="user" >
select * from user WHERE username=#{username} AND password=#{password}
</select>
</mapper>
接口中有多个参数时,接口参数类型前用注解:@Param(“和占位符一致”)
List<User> fig(@Param("username") String username, @Param("password") String password);
接口
public interface UserMapper {
//查询所有
List<User> findAll();
//根据Id查询
User findById(Long id);
//添加
int add(User user);
//删除
int del(Long id);
//修改
int chg(User user);
//多条件查询
List<User>findByCondition(User user);
List<User> findByIds(List<Long> ids);
//根据用户名和密码查询 使用注解@param
List<User> fig(@Param("username") String username, @Param("password") String password);
}
public class MyTest {
private SqlSession session;
//在所有方法前运行
@Before
public void init() throws IOException {
//使用MyBatis进行查询操作
//加载核心配置文件
InputStream stream = Resources.getResourceAsStream("SqlMapConfig.xml");
//获得sqlSession工厂对象
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
//获取Session
session = factory.openSession();
}
//在所有方法后运行
@After
public void destory(){
//释放资源
session.close();
}
//其中一个测试方法
@Test
public void testfindByIds(){
UserMapper userMapper = session.getMapper(UserMapper.class);
List<Long> ids = Arrays.asList(1L,4L,10L);
List<User> users = userMapper.findByIds(ids);
for (User user : users) {
System.out.println(user);
}
}
最后记得连数据库,导包等