概述
- 持久层
public class TeacherMapperImpl implements TeacherMapper {
@Override
//查看全部
public List<teacher> teacherAll() {
//提高作用域
InputStream Stream=null;
SqlSessionFactory build=null;
SqlSession session=null;
List<teacher> list=null;
try {
//加载配置文件
Stream = Resources.getResourceAsStream("MyBatisConfig.xml");
//获取工厂对象
build = new SqlSessionFactoryBuilder().build(Stream);
//获取SQL执行者对象
session = build.openSession(true);
//执行SQL语句
list = session.selectList("StudnetMapper.selectAll");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (Stream!=null){
session.close();
}
if (session!=null){
session.close();
}
}
return list;
}
@Override
//根据id进行查看
public teacher teacherOne(Integer id) {
//提高作用域
InputStream Stream=null;
SqlSessionFactory build=null;
SqlSession session=null;
teacher one = null;
try {
//加载配置文件
Stream = Resources.getResourceAsStream("MyBatisConfig.xml");
//获取工厂对象
build = new SqlSessionFactoryBuilder().build(Stream);
//获取SQL执行者对象
session = build.openSession(true);
//执行SQL语句
one = session.selectOne("StudnetMapper.selectSingle",1);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (Stream!=null){
session.close();
}
if (session!=null){
session.close();
}
}
return one;
}
@Override
//新增数据
public Integer intset(teacher tea) {
//提高作用域
InputStream Stream=null;
SqlSessionFactory build=null;
SqlSession session=null;
Integer result = null;
try {
//加载配置文件
Stream = Resources.getResourceAsStream("MyBatisConfig.xml");
//获取工厂对象
build = new SqlSessionFactoryBuilder().build(Stream);
//获取SQL执行者对象
session = build.openSession(true);
//执行SQL语句
result = session.insert("StudnetMapper.insert");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (Stream!=null){
session.close();
}
if (session!=null){
session.close();
}
}
return result;
}
@Override
//修改数据
public Integer update(teacher tea) {
//提高作用域
InputStream Stream=null;
SqlSessionFactory build=null;
SqlSession session=null;
Integer result = null;
try {
//加载配置文件
Stream = Resources.getResourceAsStream("MyBatisConfig.xml");
//获取工厂对象
build = new SqlSessionFactoryBuilder().build(Stream);
//获取SQL执行者对象
session = build.openSession(true);
//执行SQL语句
result = session.update("StudnetMapper.update",tea);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (Stream!=null){
session.close();
}
if (session!=null){
session.close();
}
}
return result;
}
@Override
//删除数据
public Integer delete(Integer id) {
//提高作用域
InputStream Stream=null;
SqlSessionFactory build=null;
SqlSession session=null;
Integer result = null;
try {
//加载配置文件
Stream = Resources.getResourceAsStream("MyBatisConfig.xml");
//获取工厂对象
build = new SqlSessionFactoryBuilder().build(Stream);
//获取SQL执行者对象
session = build.openSession(true);
//执行SQL语句
result = session.delete("StudnetMapper.delete",id);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (Stream!=null){
session.close();
}
if (session!=null){
session.close();
}
}
return result;
}
}
- 业务层
public class TeacherServiceImpl implements TeacherServic {
//创建dao层对象
private TeacherMapper mapper = new
TeacherMapperImpl();
@Override
//查看全部
public List<teacher> teacherAll() {
return mapper.teacherAll();
}
@Override
//根据id进行查询
public teacher teacherOne(Integer id) {
return mapper.teacherOne(id);
}
@Override
//新增数据
public Integer intset(teacher tea) {
return mapper.intset(tea);
}
@Override
//修改数据
public Integer update(teacher tea) {
return mapper.update(tea);
}
@Override
//删除数据
public Integer delete(Integer id) {
return mapper.delete(id);
}
}
- 控制层
public class teacherController {
//创建对象
private TeacherServic tea = new TeacherServiceImpl();
teacher t = new teacher(2,"武则天",36);
@Test
//查询全部
public void teacherAll(){
List<teacher> list = tea.teacherAll();
for (teacher teacher : list) {
System.out.println(teacher);
}
}
@Test
//查询单个
public void teacherOne(){
teacher te = tea.teacherOne(1);
System.out.println(te);
}
@Test
//新增数据
public void inset(){
Integer intset = tea.intset(t);
System.out.println(intset);
}
@Test
//修改数据
public void update(){
Integer update = tea.update(t);
System.out.println(update);
}
@Test
//删除数据
public void delete(){
Integer delete = tea.delete(2);
System.out.println(delete);
}
}
- 日志配置文件
# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
#打印日志级别
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
- 映射文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="StudnetMapper">
<!--
查询全部-->
<select id="selectAll" resultType="com.it.heima.mybatisv.teacher">
SELECT*FROM teacher
</select>
<!--
根据条件查询单个数据-->
<!--
parameterType 属性指条件参数类型,写包全名-->
<select id="selectSingle" resultType="teacher" parameterType="int">
--
括号里的id仅仅是为参数取个名称
SELECT*FROM teacher where id = #{id}
</select>
<!--
新增数据返回影响行数,故resultType属性封装对象可省不写-->
<!--
新增参数数据类型写自定义类的全包名-->
<insert id="insert" parameterType="com.it.heima.mybatisv.teacher">
INSERT * INTO teacher VALUES (#{id},#{name},#{age})
</insert>
<!--
修改数据-->
<update id="update" parameterType="com.it.heima.mybatisv.teacher">
UPDATE teacher SET NAME = #{name},age=#{age} where id= #{id};
</update>
<!--
删除功能-->
<delete id="delete" parameterType="java.lang.Integer">
delete from teacher where id = #{id}
</delete>
</mapper>
- 核心配置文件
<?xml version="1.0" encoding="UTF-8" ?>
<!--MyBatis的DTD约束-->
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--
引入数据库连接文件-->
<properties resource="jdbc.properties"></properties>
<!--
起别名-->
<typeAliases>
<typeAlias type="com.it.heima.mybatisv.teacher" alias="teacher"></typeAlias>
</typeAliases>
<environments default="mysql">
<environment id="mysql">
<transactionManager type="JDBC"></transactionManager>
<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 resource="StudentMapper.xml"/>
</mappers>
</configuration>
最后
以上就是大方口红为你收集整理的三层架构、增删改查的全部内容,希望文章能够帮你解决三层架构、增删改查所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复