光之使者 发表于 2024-5-15 13:46:20

mybatis案例步伐

前置工作


[*]导包(mysql-connector-java、mybatis)
[*]实体类
Mapper层

1.接口

public interface BookMapper {
    public Book getBookById(Integer bookID);
}2.创建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="com.ylzl.mapper.BookMapper">
    <select id="getBookById" resultType="com.ylzl.pojo.Book">
      select * from books where bookID=#{bookID}
    </select>
</mapper>Service层

可以不利用Service层,利用其他方式包含以下核心代码也可以。
核心代码

static {
String resource = "mybatis-config.xml";
try {
    InputStream inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
    e.printStackTrace();
}
}

//1.读取xml配置文件,创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
//2.通过创建SqlSessionFactory对象获取SqlSession对象
SqlSession sqlSession = sqlSessionFactory.openSession();
//3.通过SqlSession对象获取代理对象mapper,传入接口class对象
BookMapper mapper=sqlSession.getMapper(BookMapper.class);
//4.通过mapper调用接口对应方法
mapper.getBookById(bookID);以下为利用Service层实现的方式
1.接口

public interface BookService {
    Book getBookById(Integer bookID);
}2.接口实现类(重点关注)

public class BookServiceImpl implements BookService{
    static InputStream inputStream = null;
    static {
      String resource = "mybatis-config.xml";
      try {
            inputStream = Resources.getResourceAsStream(resource);
      } catch (IOException e) {
            e.printStackTrace();
      }
    }
    //1.读取xml配置文件,创建SqlSessionFactory对象
    private final SqlSessionFactory sqlSessionFactory
            = new SqlSessionFactoryBuilder().build(inputStream);

    @Override
    public Book getBookById(Integer bookID) {
      //2.通过创建SqlSessionFactory对象获取SqlSession对象
      SqlSession sqlSession = sqlSessionFactory.openSession();
      //3.通过SqlSession对象获取代理对象mapper,传入接口class对象,
      BookMapper mapper = sqlSession.getMapper(BookMapper.class);
      //4.通过mapper调用接口对应方法
      returnmapper.getBookById(bookID);
    }
}Mybatis设置文件mybatis-config.xml(重点关注)

<?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.jdbc.Driver"/>
                <property name="url" value="jdbc:mysql://localhost:3306/ssmbuild"/>
                <property name="username" value="root"/>
                <property name="password" value="root"/>
            </dataSource>
      </environment>
    </environments>

   
    <mappers>
      <mapper resource="com/ylzl/mapper/BookMapper.xml"/>
    </mappers>
</configuration>测试

BookService bookServicelmpl = new BookServiceImpl();
System.out.println(bookServicelmpl.getBookById(2));碰到的问题

1.BookMapper.xml不放在resources下,加过滤

<build>
      <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                  <include>**/*.properties</include>
                  <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                  <include>**/*.properties</include>
                  <include>**/*.xml</include>
                </includes>
                <filtering>true</filtering>
            </resource>
      </resources>
    </build>2.解决xml编码问题:1 字节的 UTF-8 序列的字节 1 无效:https://www.cnblogs.com/thetree/p/12991403.html
转载请注明原文链接:https://www.cnblogs.com/yulingzhiling/p/18099119若标题中有“转载”字样,则本文版权归原作者全部。若无转载字样,本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保存此段声明,且在文章页面显着位置给出原文毗连,否则保存追究法律责任的权利.
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: mybatis案例步伐