domain包(bean)
配置文件
核心配置文件MyBatisConfig.xml
引入jdbc.properties
引入log4j.properties
配置映射关系
<!--配置映射关系-->
<mappers>
<package name="com.itheima.mapper"/>
</mappers>
mapper包(dao)
service包
mapper层
public interface StudentMapper {
//修改操作
@Update("UPDATE student SET name=#{name},age=#{age} WHERE id=#{id}")
public abstract Integer update(Student stu);
}
service层
public interface StudentService {
void update();
}
public class StudentServiceImpl {
public void update() throws Exception{
//1.加载核心配置文件
InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
//2.获取SqlSession工厂对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//3.通过工厂对象获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession(true);
//4.获取StudentMapper接口的实现类对象
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//5.调用实现类对象中的方法,接收结果
Student stu = new Student(4,"赵六",36);
Integer result = mapper.update(stu);
//6.处理结果
System.out.println(result);
//7.释放资源
sqlSession.close();
is.close();
}
}
7 Mybatis - 接口代理实现dao + 注解 开发 - 单表(案例)
原文:https://www.cnblogs.com/60kmph/p/14051525.html