mybatis配置步骤
第一步:pom.xml
第二步:application.properties配置数据源
#数据源 spring.datasource.url=jdbc:mysql://localhost:3306/mybatis-demo?useUnicode=true&characterEncoding=utf-8 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.password=admine spring.datasource.username=root
第三步:建立各层包
此处忽略不计
第四步:application.properties配置mybatis扫描映射
#mapper映射 mybatis.mapper-locations=classpath:mapper/*.xml #mybatis.type-aliases-package=com.example.mybatis.demo.entity #sql日志 logging.level.com.example.mybatis.demo.mapper=debug
第五步:启动类上添加持久层接口路径
扩展步骤:mapper.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="com.example.mybatis.demo.mapper.UserMapper"> <resultMap id="ResultBeanMap" type="com.example.mybatis.demo.entity.User"> <result property="id" column="id" jdbcType="DECIMAL"></result> <result property="name" column="name" jdbcType="VARCHAR"></result> <result property="age" column="age" jdbcType="DECIMAL"></result> </resultMap> <select id="findAll" resultMap="ResultBeanMap"> SELECT * FROM user; </select> </mapper>
本文来自博客园,作者:土木转行的人才,转载请注明原文链接