成都创新互联网站制作重庆分公司

Java开发之Mybatis框架

mybasits配置文件书写
1.configer文件配置

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">





















2.mapper文件配置
(1)resultType是返回值类型
(2)pramatetertype是传入的参数

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">







insert into table4012(id,username,number,mybasits,android,javaee) values (#{id},#{username},#{number},#{mybasits},#{android},#{javaee})


delete from table4012 where id=#{**}


update table4012 set username=#{username},number =#{number},mybasits=#{mybasits},android=#{android},javaee=#{javaee} where id=#{id}




3.util类配置
package com.SelfStudy.util;
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.InputStream;
public class Util {
private static SqlSessionFactory build;//定义一个对象
static {
String s="mybatis.config.xml";//加载config文件
InputStream inputStream=null;//输入输出流
try {
inputStream= Resources.getResourceAsStream(s);
build=new SqlSessionFactoryBuilder().build(inputStream);
}
catch (Exception e){
e.printStackTrace();
}
finally {
try {
if ( inputStream !=null){
inputStream.close();
}
}catch (Exception e){
e.printStackTrace();
}
}
}
public static SqlSession getSession(){
return build.openSession();
}
}
4.log4j.properties文件配置
log4j.appender.console =org.apache.log4j.ConsoleAppender
log4j.appender.console.Target =System.out
log4j.appender.console.layout =org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern =[%-5p][%d{yyyy-MM-dd HH:mm:ss}] %c %L %m%n

专注于为中小企业提供成都网站制作、成都做网站、外贸营销网站建设服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业梁河免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了成百上千企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。

\u914D\u7F6E\u6839

log4j.rootLogger =debug,console
##log4j.logger.com.mapper.StudentMapper=debug,console
四.动态sql
1.if set标签的使用
Select from table
if(test="name!=null and name!=''"){
#{id}
}
如果是数据库添加语句在每一句后面添加,
if(test="name!=null and name!=''"){
#{id},
}
2.where标签的使用
Select
from table

if(test="name!=null and name!=''"){
name=#{name}
}
如果是数据库添加语句在每一句后面添加,
if(test="id!=null and id!=''"){
id=#{id}
}

3.choose when标签的使用



and name=#{name}



  1. set标签的使用
    uddate Table
    set id=#{id}


  2. trim标签的使用

    update Teacher

    name=#{name},age=#{age},

    where id=#{id}

    prefix=“在前面进行添加”
    prefixOverrides=“在前面进行去掉”
    suffix=“在后面进行添加”
    suffixOverrides=“在后面进行去掉”
    重点:执行的顺序 先去除再添加
  3. bind标签

    slct * from tabl wr nam lik #{nam}
  4. foreach(集合查询,添加)

    insert into table () valuse

    #{},


    collection:传入的参数类型
    open:打开方式
    close:关闭方式
    item:item
  5. selectkey 的使用(查询上一条记录的一个属性)

    insert table valuse()

    select last_insert_name()

    keypropert是查询的属性的名称
    resulttype是XM代理申请www.fx61.com/brokerlist/xm.html返回值类型,是查询结果的返回值类型
    order是执行顺序
    keycolum:数据库中对应的属性
    多种查询方法的使用
    1.模糊查询
    select from table where name=#{name}
    [1]在查询的时候改正
    SelectAll("%ko%");
    [2]在sql语句中改正
    使用concat函数
    select
    from table where
    id=concat('%',#{id},'%')
  6. 多表联合查询
    (1) mapper文件的配置类

    //封装连接类的属性,主要是主类属性




    //用association封装子类所有的属性






    (2) test测试类@org.junit.Test
    br/>@org.junit.Test
    SqlSession session=Util.getSession();
    PeopleMapperDao peopleMapperDao=session.getMapper(PeopleMapperDao.class);
    peopleMapperDao.SelectTogether();
    List list=null;
    list=peopleMapperDao.SelectTogether();
    System.out.println(list);
    }
    3.一对一联合查询
    配置类文件
    package com.pojo;
    public class Student {
    private String name;
    private Integer id;
    private Integer tid;//和老师中的id对因
    private String address;
    private String city;
    //一个学生对应一个老师
    private Teacher teacher;//在学生中查询老师
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public Integer getTid() {
    return tid;
    }
    public void setTid(Integer tid) {
    this.tid = tid;
    }
    public String getAddress() {
    return address;
    }
    public void setAddress(String address) {
    this.address = address;
    }
    public String getCity() {
    return city;
    }
    public void setCity(String city) {
    this.city = city;
    }
    public Teacher getTeacher() {
    return teacher;
    }
    public void setTeacher(Teacher teacher) {
    this.teacher = teacher;
    }
    public Student(String name, Integer id, Integer tid, String address, String city, Teacher teacher) {
    this.name = name;
    this.id = id;
    this.tid = tid;
    this.address = address;
    this.city = city;
    this.teacher = teacher;
    }
    public Student() { }@Override
    br/>@Override
    return "Student{" +
    "name='" + name + '\'' +
    ", id=" + id +
    ", tid=" + tid +
    ", address='" + address + '\'' +
    ", city='" + city + '\'' +
    ", teacher=" + teacher +
    '}';
    }
    }
    接口配置
    package com.service;
    import com.pojo.Student;
    import java.util.List;
    public interface StudentDao {
    public List Selectall();
    public List SelectByid(Integer id);
    //查询所有学生
    public List SelectAllStudent();
    public List selct01();
    }
    mapper文件配置

    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">











    4.多对一联合查询













  7. 多对多的联合查询
    实体类的配置
    package com.pojo;
    import java.util.Date;
    import java.util.List;
    public class Writer {
    private String name;
    private Integer id;
    private Integer age;
    private String sex;
    private String book;
    private Date birthday;
    //查询作者里面包含作品
    private List works;
    public List getWorks() {
    return works;
    }
    public void setWorks(List works) {
    this.works = works;
    }
    public Writer(List works) {
    this.works = works;}
    @Override
    br/>}
    @Override
    return "Writer{" +
    "name='" + name + '\'' +
    ", id=" + id +
    ", age=" + age +
    ", sex='" + sex + '\'' +
    ", book='" + book + '\'' +
    ", birthday=" + birthday +
    ", works=" + works +
    '}';
    }
    public String getName() {
    return name;
    }
    public void setName(String name) {
    this.name = name;
    }
    public Integer getId() {
    return id;
    }
    public void setId(Integer id) {
    this.id = id;
    }
    public Integer getAge() {
    return age;
    }
    public void setAge(Integer age) {
    this.age = age;
    }
    public String getSex() {
    return sex;
    }
    public void setSex(String sex) {
    this.sex = sex;
    }
    public String getBook() {
    return book;
    }
    public void setBook(String book) {
    this.book = book;
    }
    public Date getBirthday() {
    return birthday;
    }
    public void setBirthday(Date birthday) {
    this.birthday = birthday;
    }
    public Writer(String name, Integer id, Integer age, String sex, String book, Date birthday) {
    this.name = name;
    this.id = id;
    this.age = age;
    this.sex = sex;
    this.book = book;
    this.birthday = birthday;
    }
    public Writer() { }
    }
    mapper文件配置

    PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
    "http://mybatis.org/dtd/mybatis-3-mapper.dtd">




















    注解方式配置
    @Results(value={
    br/>

    注解方式配置
    @Results(value={
    br/>@Result(column="",property=""),
    br/>@Result(column="",property=""),
    br/>@Result(column="",property=""),
    @Select(".......")
    br/>})
    @Select(".......")
  8. 导入包

    org.apache.poi
    poi
    3.17



    commons-io
    commons-io
    2.4

    //导入依赖
    2.构建方法
    public class ImportExcel {
    //导入excel文件的方法
    public void ReadExcel(File file){
    List list=new ArrayList<>();
    //1.传入需要导入的Excel文件的路径
    try{
    //2.读取文件的内容
    HSSFWorkbook workbook=new HSSFWorkbook(FileUtils.openInputStream(file));
    //3.读取文件的sheet页,sheet就是Excel中的每一张表,也就是workboo的sheet页
    HSSFSheet sheet=workbook.getSheetAt(0);
    //4.开始读取表的内容
    int startline=0;//起始行的行数,也就是从0 到最后一行
    int endline=sheet.getLastRowNum()+1;//最后一行d的下表
    //遍历每一行
    Map> map=new HashMap<>();
    //第一行,第一列,值
    //遍历行
    for (int a=1;aHSSFRow hssfRow=sheet.getRow(a);
    //遍历列
    //写一个集合存储列和值
    Map result=new HashMap<>();
    int endcell=hssfRow.getLastCellNum();
    for (int j=0;jresult.put(j, hssfRow.getCell(j));//用行数去获取列
    }
    map.put(a, result);
    }
    List list1=new ArrayList<>();
    for (Integer xxx:map.keySet()){
    System.out.print(map.get(xxx).get(0));
    System.out.print(map.get(xxx).get(1));
    System.out.print(map.get(xxx).get(2));
    System.out.print(map.get(xxx).get(3));
    People people=new People();
    people.setName(String.valueOf(map.get(xxx).get(0)));
    String java=String.valueOf(map.get(xxx).get(1));
    Integer java1=Integer.parseInt(java.substring(0, java.indexOf(".")));
    people.setJava(java1);
    String mybas=String.valueOf(map.get(xxx).get(2));
    Integer mybas1=Integer.parseInt(mybas.substring(0, mybas.indexOf(".")));
    people.setMybas(mybas1);
    String androi=String.valueOf(map.get(xxx).get(3));
    Integer androi1=Integer.parseInt(androi.substring(0, androi.indexOf(".")));
    people.setAndroi(androi1);
    list1.add(people);
    }
    SqlSession session= Util.getSession();
    PeopleMapperDao peopleMapperDao=session.getMapper(PeopleMapperDao.class);
    peopleMapperDao.ExcelInsert(list1);
    session.commit();
    }
    catch (Exception e){
    e.printStackTrace();
    }
    }
    }
  9. 调用方法@org.junit.Test
    br/>@org.junit.Test
    ImportExcel aaa=new ImportExcel();
    File file=new File("C:/Users/lenovo/Desktop/study/ssm笔记/test4012.xls");
    aaa.ReadExcel(file);
    }

标题名称:Java开发之Mybatis框架
标题来源:http://cxhlcq.com/article/pocssh.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部