项目记录一(eclipse到idea,Hibernate到Mybatis)

项目场景:

以前用的eclipse做了一个泥土配比器的项目,用的老的SSH架构。现如今用了IDEA,也学了点mybatis,就用这个试一试。

经历过程:

一、eclipse到idea

(1)在idea中新建maven项目——>问题:如何将新建的maven项目带有web功能
(2)将pom.xml中的< packaging >jar< /packaging >改为war,在项目目录中新建web目录在这里插入图片描述

(3)在新建的maven项目中单击右上角的在这里插入图片描述
将这项改为web目录中的classes,classes变为黄色在这里插入图片描述
(4)在右上角添加Tomcat,并在如下图位置添加你的项目
在这里插入图片描述
(5)之后将eclipse中对应目录的代码复制到idea中就可以了。

总结:
1.新建maven项目的环境要和eclipse中的一样(jdk,maven,Tomcat)。
2.lib中有jar包时,要在Project Structure->Modules->Dependies选择Jars or directories添加lib包。
3.注意修改pom.xml中的jar为war,否则Tomcat中找不到项目。
发展:
1.尝试将eclipse项目直接导入到idea中。


二、hibernate到mybatis

(1)将原项目中的xxxx.hbm.xml、hibernate.cfg.xml和pom.xml中的hibernate引用删除。

(2)在pom.xml中添加mybatis引用

		<dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
		</dependency>

(3)在Java和resources目录中创建同层文件(名称要一样)
在这里插入图片描述

(4)创建配置文件,并编写配置文件
在这里插入图片描述

(5)编写对应的接口和xml文件
接口文件IShopBeanDao:

public interface IShopBeanDao {
    
    
	 boolean deleteShopBean(int id);
	 List<ShopBean> getAllList();
	 ShopBean getShopById (int id);
}

IShopBeanDao.xml文件:

<?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="cn.demo.dao.IShopBeanDao">

    <!-- 配置 查询结果的列名和实体类的属性名的对应关系 -->
    <resultMap id="ShopBeanMap" type="ShopBean">
        <!-- 主键字段的对应 -->
        <id property="id" column="id"></id>
        <!--非主键字段的对应-->
        <result property="companyName" column="companyName"></result>
        ...等等...
    </resultMap>

    <!-- 查询所有 -->
    <select id="getAllList" resultMap="ShopBeanMap">
        select * from shopbean order by id;
    </select>
    <!-- 删除-->
    <delete id="deleteShopBean" parameterType="java.lang.Integer">
        delete from shopbean where id = #{id}
    </delete>
    <!-- 根据id查询用户 -->
    <select id="getShopById" parameterType="INT" resultMap="ShopBeanMap">
        select * from shopbean where id = #{id}
    </select>
</mapper>

(6)在test->java创建简单的测试类,测试能否读取数据

public class MybatisTest {
    
    

    private InputStream in;
    private SqlSession sqlSession;
    private IShopBeanDao shopBean;

    @Before//用于在测试方法执行之前执行
    public void init()throws Exception{
    
    
        //1.读取配置文件,生成字节输入流
        in = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.获取SqlSessionFactory
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
        //3.获取SqlSession对象
        sqlSession = factory.openSession();
        //4.获取dao的代理对象
        shopBean =  sqlSession.getMapper(IShopBeanDao.class);
    }

    @After//用于在测试方法执行之后执行
    public void destroy()throws Exception{
    
    
        //提交事务
        sqlSession.commit();
        //6.释放资源
        sqlSession.close();
        in.close();
    }

    /**
     * 测试查询所有
     */
    @Test
    public void testFindAll(){
    
    
        //5.执行查询所有方法
        List<ShopBean> shopBeans = shopBean.getAllList();
        for(ShopBean s : shopBeans){
    
    
            System.out.println(s);
        }
        new SessionFactory().destroyFactory(sqlSession);

    }
 }

(7)将其余的接口和xml文件补全。
1.解决保存过程中id主键的自增长和返回显示

<!-- 保存 -->
    <insert id="insert" parameterType="pbBean">
        <!-- 配置插入操作后,获取插入数据的id -->
        <selectKey keyProperty="id" keyColumn="id" resultType="int" order="AFTER">
            select last_insert_id();
        </selectKey>
        insert into pbBean(conpanyid,conpanyname,peibi,summoney)values(#{conpanyid},#{conpanyname},#{peibi},#{summoney});
    </insert>

2.解决一个xml文件中多个对象的保存和查询
创建多个< resultMap >< /resultMap >

    <!-- 配置 pbBean -->
    <resultMap id="pbBeanMap" type="pbBean">
        <!-- 主键字段的对应 -->
        <id property="id" column="id"></id>
        <!--非主键字段的对应-->
        <result property="conpanyid" column="conpanyid"></result>
        ***等等***
    </resultMap>

    <!-- 配置 PeiBiMuLu -->
    <resultMap id="PeiBiMuLuMap" type="PeiBiMuLu">
        <!-- 主键字段的对应 -->
        <id property="id" column="id"></id>
        <!--非主键字段的对应-->
        <result property="PeiBiMingZi" column="peiBiMingZi"></result>
        ***等等***
    </resultMap>

3.解决mybatis中使用多条件查询功能(一个对象,一个String)
在接口文件中修改方法

List<PeibiBiao> getPeiBiBiao(@Param("testbean")testbean testBean,@Param("biaoDan_id")String biaoDan_id);

在xml文件中修改查询方法

<!-- 查询所有 -->
    <select id="getPeiBiBiao" resultMap="PeibiBiaoMap">
        select * from PeibiBiao where na = #{testbean.na} and fenShuId = #{biaoDan_id} order by summoney;
    </select>

(8)创建SessionFactory工厂并修改对应的Java文件
SessionFactory.java

public class SessionFactory {
    
    
    public SqlSession initFactory(){
    
    
        InputStream in;
        try {
    
    
            //1.读取配置文件,生成字节输入流
            in = Resources.getResourceAsStream("SqlMapConfig.xml");
            //2.获取SqlSessionFactory
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
            in.close();
            //3.获取SqlSession对象
            return factory.openSession();
        } catch (Exception e){
    
    
            System.out.println("session获取失败");
        }
        return null;
    }
    public void destroyFactory(SqlSession sqlSession){
    
    
        System.out.println("关闭");
        //4.提交事务
        sqlSession.commit();
        //6.释放资源
        sqlSession.close();
    }
}

在其他Java文件中使用:

public class ShopAction {
    
    
    private SqlSession sqlSession;
	private IShopBeanDao IshopBean;
	public List<ShopBean> findAll(){
    
    
//	    1.获取session
		sqlSession = new SessionFactory().initFactory();
//		2.代理对象
		IshopBean = sqlSession.getMapper(IShopBeanDao.class);
//		3.业务逻辑
        List<ShopBean> shopBeans = IshopBean.getAllList();
//      4.结束session
        new SessionFactory().destroyFactory(sqlSession);
		return shopBeans;
	}
}

总结:
1.在resources中创建xml的目录时一层一层建(❌cn.demo.dao这样是错误的),最好不要选择折叠目录,取消如图勾选
在这里插入图片描述
2.mybatis连接数据库时在数据库中加上useUnicode=true&&characterEncoding=utf-8
防止乱码
3.编写xml文件时方法中的resultMap="PeibiBiaoMap"是从数据中接收信息的填写对应resultMap的id;parameterType="INT"是要输入方法中的变量,如果是对象填写resultMap的Type。
4.编写xml方法时sql语句中的<、>等符号时借鉴下方图
在这里插入图片描述

5.在xml中编写保存方法时,对象的id是主键时,尽量不要在sql中写id
6.修改其他Java文件是注意sqlSession关闭时机

发展:
1.用注释的方法实现
2.修改顺序时,先编写工具类->编写简单测试类->修改其余Java文件->编写接口对应的xml文件。
3.编写对象时注意命名规范


项目链接:

https://blog.csdn.net/qq_38922932/article/details/108608045

猜你喜欢

转载自blog.csdn.net/qq_38922932/article/details/108540150