09.JavaWeb-MyBatis

3.MyBatis        

        MyBatis 是一款优秀的持久层框架,它支持自定义 SQL、存储过程以及高级映射。MyBatis 免除了几乎所有的 JDBC 代码以及设置参数和获取结果集的工作。

3.1 配置MyBatis

3.1.1 导入依赖

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.33</version>
    </dependency>
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.5.6</version>
    </dependency>

3.1.2 配置MyBatis的xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
  <environments default="development">
    <environment id="development">
      <transactionManager type="JDBC"/>
      <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="org/mybatis/example/BlogMapper.xml"/>
  </mappers>
</configuration>

3.1.3 配置mapper相应的xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "https://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.mybatis.example.BlogMapper">
  <select id="selectBlog" resultType="Blog">
    select * from Blog where id = #{id}
  </select>
</mapper>

3.2使用MyBatis

        在要使用mybatis的类中操作

3.2.1 加载mybatis主配置文件

String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);

3.2.2 创建工厂对象

 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

3.2.3 创建SqlSession对象

                用来调用sql语句操作数据库

SqlSession sqlSession = sqlSessionFactory.openSession();

3.2.4 创建Mapper相应的接口

        注意:
        * 1.方法的名字与对象sql的id保持一致
        * 2.对应xml的namespace值必须是对应接口的类型全名

3.2.5 接口中创建需要的方法(crud)

    List<Employee> findAll();
    Employee findById(int id);
    Employee findByIdAndName(Map<String,Object> map);
    Employee findByIdAndName(Employee employee);

    //@Param()其实就是将参数封装为map对象,注解的内容作为key,参数的值作为值
    Employee findByIdAndName(@Param("id")int id,@Param("name") String name);

    //增
    boolean add(Employee employee);
    //改
    void update(@Param("id") int id,@Param("name") String name);

3.2.6 mapper相应的xml文件创建相应的sql语句

<!--namespace:命名空间,用来避免id相同的sql语句冲突,一般使用一个完整的类名-->
<mapper namespace="com.woniuxy.mapper.EmployeeMapper">
    <!--select查询   id:sql语句的唯一标识符,同一个xml中不能出现id相同的;sqlresultType:将查询出来的数据自动封装成什么类型的数据,需要类型全名,因为mybatis底层通过Class.forName来得到字节码对象-->
    <!--如果sql只有一个参数,而且是基本数据类型、string,参数名只起到占位作用,随便取如果传入的参数是map,则应该#{属性名}-->
    <select id="findByIdAndName" resultType="com.woniuxy.entity.Employee">
        select * from employee where id = #{id} and name = #{name}
    </select>

    <insert id="add">
        insert into employee values(null,#{cellphone},#{password},#{avatar},#{name},'0');
    </insert>
    <update id="update">
        update employee set name = #{name} where id = #{id}
    </update>

3.2.7 通过sqlSession得到接口实现的对象

EmployeeMapper mapper = sqlSession.getMapper(EmployeeMapper.class);

 3.2.8 调用接口的方法实现相应的操作

        //5.单参
        Employee byId = mapper.findById(1);
        System.out.println(byId);

        //6.多参
        Map<String,Object> map = new HashMap<>();
        map.put("id",1);
        map.put("name","小李");
        Employee byIdAndName = mapper.findByIdAndName(map);
        System.out.println("map"+byIdAndName);

        Employee arg = new Employee();
        arg.setId(1);
        arg.setName("小李");
        Employee byIdAndName1 = mapper.findByIdAndName(arg);
        System.out.println("arg:"+byIdAndName1);

3.3 传参的三种方法

        mysql不支持多个参数单独传递,只能一个数据或者多个数据(map、entity、array、list)

    Employee findByIdAndName(Map<String,Object> map);
    Employee findByIdAndName(Employee employee);

    //@Param()其实就是将参数封装为map对象,注解的内容作为key,参数的值作为值
    Employee findByIdAndName(@Param("id")int id,@Param("name") String name);

        实例对象与注解的方法比较好用,map一般不用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java EE 项目的目录结构可以根据具体的需求进行灵活设计,但一般情况下,推荐使用以下的标准目录结构: ``` project ├── src │ ├── main │ │ ├── java │ │ │ └── com │ │ │ └── example │ │ │ ├── controller │ │ │ ├── dao │ │ │ ├── entity │ │ │ ├── service │ │ │ └── util │ │ ├── resources │ │ │ ├── mapper │ │ │ └── db.properties │ │ └── webapp │ │ ├── WEB-INF │ │ │ ├── classes │ │ │ ├── lib │ │ │ └── web.xml │ │ ├── css │ │ ├── js │ │ ├── images │ │ └── index.jsp │ └── test │ ├── java │ └── resources ├── target ├── pom.xml └── README.md ``` 其中,各个目录的作用如下: - `src/main/java`:存放项目的 Java 源代码,按照包名分层,一般包括 `controller`、`dao`、`entity`、`service` 和 `util` 等包; - `src/main/resources`:存放项目的配置文件和资源文件,一般包括数据库连接配置文件 `db.properties`、MyBatis 的 mapper 文件等; - `src/main/webapp`:存放 Web 应用的 Web 资源,包括 JSP 页面、CSS 样式表、JavaScript 脚本等; - `src/test/java`:存放项目的测试代码; - `src/test/resources`:存放测试代码所需要的资源文件; - `target`:存放编译后的 .class 文件、打包后的 .war 文件等; - `pom.xml`:Maven 项目管理工具的配置文件; - `README.md`:项目说明文件。 以上是一种常见的 Java EE 项目目录结构,但并不是唯一的标准。在实际开发中,可以根据项目的具体需求进行合理的调整和修改。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值