三层架构 界面层(controller—springmvc):和用户打交道,接受用户的请求参数,显示处理的结果
业务逻辑层(service—spring):接受页面的数据,计算逻辑,调用数据库,获取数据
访问数据层(dao—mybaits):访问数据库,实现增删改查
mybaits:
sql mapper:sql映射
可以把数据库中一行数据,映射为一个Java对象,一行数据可以看作一个Java对象,操作这个对象,就相当于操作表中的数据。
Data Access Obiects: 数据访问,对数据库实现增删改查
功能:
提供了创建connection,statement等能力
提供了执行sql语句的能力
提供的sql循环,把sql结果转换为Java对象,list集合的能力
提供了关闭资源的能力
入门案例 数据库建立表
导入jar包
书写student类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 package com.zss.pojo;public class Student { Integer id; String name; String email; Integer age; public Student () { } public Integer getId () { return id; } public void setId (Integer id) { this .id = id; } public String getName () { return name; } public void setName (String name) { this .name = name; } public String getEmail () { return email; } public void setEmail (String email) { this .email = email; } public Integer getAge () { return age; } public void setAge (Integer age) { this .age = age; } }
添加xml文件 1 2 3 4 5 6 7 8 9 10 11 12 13 <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace ="com.zss.dao.StudentDAO" > <select id ="selectStudents" resultType ="com.zss.pojo.Student" > select * from student order by id; </select > </mapper >
写入数据库链接 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 <?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 > <environments default ="development" > <environment id ="development" > <transactionManager type ="JDBC" /> <dataSource type ="POOLED" > <property name ="driver" value ="com.mysql.cj.jdbc.Driver" /> <property name ="url" value ="jdbc:mysql://localhost:3306/school" /> <property name ="username" value ="root" /> <property name ="password" value ="200101" /> </dataSource > </environment > </environments > <mappers > <mapper resource ="com/zss/dao/Student.xml" /> </mappers > </configuration >
dao类添加 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.zss.dao;import com.zss.pojo.Student;import java.util.List;public interface StudentDAO { public List<Student> selectStudents () ; }
实现类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 package com.zss;import com.zss.pojo.Student;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;import java.io.InputStream;import java.util.List;public class App { public static void main ( String[] args ) throws IOException { String config= "/mybaits.xml" ; InputStream in= Resources.class.getResourceAsStream(config); SqlSessionFactoryBuilder builder=new SqlSessionFactoryBuilder (); SqlSessionFactory factory=builder.build(in); SqlSession session= factory.openSession(); String sqlId="com.zss.dao.StudentDAO" +"." +"selectStudents" ; List<Student> studentList=session.selectList(sqlId); System.out.println(studentList); session.close(); } }
插入操作实例 1 2 3 4 5 6 7 8 String sqlId="com.zss.dao.StudentDAO" +"." +"insertStudent" ; int flag=session.insert(sqlId,new Student (1003 ,"haha" ,"zss@sina.com" ,25 )); System.out.println(flag); session.commit();