关于SSM(mybatis)

(代码小白自我总结,仅供参考)

关键词:mybatis映射与关联查询,tomcat目录,web工程目录,maven工程目录

1,mybatis映射(resultMap)与关联查询

(1)利用resultMap解决属性和字段不匹配

//--------实体类-------------------------------------------------
public class Dept{

    private Integer deptNo;
    private String dName;
    private String loc;
}

//接口
public interface DeptMapper{

//sql语句查询返回hashMap,通过resultMap解决属性和字段不一致
   List<Dept> map1();
}

//---------mapper---------------------------------------------------
<?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="edu.lq.mapper.DeptMapper">

    //id是接口中写的方法名,resultMap符合命名规范即可
    <select id="map1" resultMap="deptMap">
        select * from dept0  //sql语句
    </select>
    
    <resultMap id="DeptMap" type="Dept"> //id和上面的resultMap一致
        <id property="deptNo" cloumn="dept_no">  //property:java中的属性,cloumn:数据库中的字段
        <result property="dName" cloumn="d_name">
        <result property="loc" cloumn="loc">
    </resuleMap>
</mapper>

//----------测试test-----------------------------------------------------

public class AppTest{

private static final Logger logger = LogManager.getLogger(AppTest.class)
private SqlSessionFactory factory;

@Before
//连接factory
public void SetUp() throws Exception{
    InputStteam stream = Resources.getRecourseAsStream("mybatis-config.xml");
    factory = new SqlSessionFactoryBuilder().builder(stream);
    logger.debug("----factory初始化成功----")
}

//factory置空
@After
public void TearDown(){
    factory = null;
    logger.debug("----factory置空成功----")
}


//测试
@Test
public void map1(){
    SqlSession session = factory.openSession();
    DeptMapper mapper = session.getMapper(DeptMapper.class);
    
    List<Dept> depts = mapper.map1();
    foreach(Dept d : depts){
        System.out.println(d);
        }
    }

}

(2)结果映射(一对一查询<association/>)

//--------------------------------接口------------------------------
public interface EmpMapper {
    //一对一查询
    List<Emp> getAll();
}

//--------------------------------映射---------------------------------
<?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="edu.lq.mapper.EmpMapper">

    <select id="getAll" resultMap="EmpMap">
        select * from emp,dept
        where emp.deptno=dept.deptno
    </select>
     <resultMap id="EmpMap" type="emp" autoMapping="true">

<!--autoMapping="true"代替注释掉的代码-->
        <id property="empno" column="empno"/>
<!--        <result property="ename" column="ename"/>-->
<!--        <result property="job" column="job"/>-->
<!--        <result property="mgr" column="mgr"/>-->
<!--        <result property="hiredate" column="hiredate"/>-->
<!--        <result property="sal" column="sal"/>-->
<!--        <result property="comm" column="comm"/>-->

<!--        一对一-->
        <association property="dept" column="deptno" javaType="dept" autoMapping="true">
            <id property="deptno" column="deptno"/>
<!--            <result property="dname" column="dname"/>-->
<!--            <result property="loc" column="loc"/>-->
        </association>

    </resultMap>

</mapper>
//---------------------------------测试----------------------------
public class App 
{

    public static void main( String[] args ) throws IOException {
        InputStream stream = Resources.getResourceAsStream("mybatis-config.xml");
       SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
        SqlSession session = factory.openSession();
        EmpMapper mapper = session.getMapper(EmpMapper.class);

        List<Emp> emps = mapper.getAll();

        for (Emp emp:emps ){
            System.out.println(emp);
        }

    }
}

(3)高级映射(一对多查询<collection/>)

//--------------------接口-----------------------------
public interface DeptMapper {
    //一对多查询
    List<Dept> getAll();
}

//--------------------映射---------------------------------
<?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="edu.lq.mapper.DeptMapper">

<!--    左外连接查询-->
    <select id="getAll" resultMap="DeptMap">
        select * from dept d left join emp e
        on d.deptno=e.deptno
    </select>
    <resultMap id="DeptMap" type="dept">

        <id property="deptno" column="deptno"/>
        <result property="dname" column="dname"/>
        <result property="loc" column="loc"/>

<!--        一对多-->
        <collection property="emps" column="depton" ofType="emp">
            <id property="empno" column="empno"/>
            <result property="ename" column="ename"/>
            <result property="job" column="job"/>
            <result property="mgr" column="mgr"/>
            <result property="hiredate" column="hiredate"/>
            <result property="sal" column="sal"/>
            <result property="comm" column="comm"/>
        </collection>
    </resultMap>

</mapper>

//----------------------------测试-----------------------------
public class App 
{
public static void main( String[] args ) throws IOException {
    InputStream stream = Resources.getResourceAsStream("mybatis-config.xml");
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(stream);
    SqlSession session = factory.openSession();

    DeptMapper mapper = session.getMapper(DeptMapper.class);

    List<Dept> depts = mapper.getAll();

    for (Dept dept:depts ){
        System.out.println(dept);
    }

  }

}

2,java工程目录(tomcat)

 bin:(二进制文件)存放tomcat的可执行命令,例如启动,关闭tomcat

conf:存放tomcat的配置文件,server.xml(核心文件)

lib:存放服务器运行环境的依赖jar包

logs:存放运行过程中产生的记录,即日志文件,方便快速找到问题解决问题

temp:存放运行产生的临时文件

webapps:(重要)存放web网站,可通过http协议访问

work:存放web的jsp编译后的servlet文件,

3,web(动态)工程目录

---WEB-INF---web.xml(网站核心)

---index.jsp

4,maven工程目录

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值