Mybatis的配置及使用

    xiaoxiao2023-11-25  146

    目录

    Mybatis在pom文件中的依赖

    Mybatis-config.xml文件配置

    DataSourceConfiguration类

    SessionFactoryConfiguration类

    application.properties

    dao层创建

    mapper编写(dao层类同名xml文件)


    Mybatis在pom文件中的依赖

    <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.4</version> </dependency>

    Mybatis-config.xml文件配置

    Mybatis配置文件之属性配置元素解析

    <?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> <settings> <setting name="useGeneratedKeys" value="true"/> <setting name="useColumnLabel" value="true"/> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> </configuration>

    DataSourceConfiguration类

    @Configuration @MapperScan("com.imooc.demo.dao") public class DataSourceConfiguration { @Value("${jdbc.driver}") private String jdbcDriver; @Value("${jdbc.url}") private String jdbcUrl; @Value("${jdbc.username}") private String jdbcUsername; @Value("${jdbc.password}") private String jdbcPassword; @Bean(name="dataSource") public ComboPooledDataSource createDataSource() throws PropertyVetoException { ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass(jdbcDriver); dataSource.setJdbcUrl(jdbcUrl); dataSource.setUser(jdbcUsername); dataSource.setPassword(jdbcPassword); dataSource.setAutoCommitOnClose(false); return dataSource; } }

    SessionFactoryConfiguration类

    @Configuration public class SessionFactoryConfiguration { @Value("${mybatis_config_file}") private String mybatisConfigFilePath; @Value("${mapper_path}") private String mapperPath; @Autowired @Qualifier("dataSource") private DataSource dataSource; @Value("${entity_package}") private String entityPackage; @Bean(name="sqlSessionFactory") public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException { SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean(); sqlSessionFactoryBean.setConfigLocation(new ClassPathResource(mybatisConfigFilePath)); PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(); String packageSearchPath = PathMatchingResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + mapperPath; sqlSessionFactoryBean.setMapperLocations(resolver.getResources(packageSearchPath)); sqlSessionFactoryBean.setDataSource(dataSource); sqlSessionFactoryBean.setTypeAliasesPackage(entityPackage); return sqlSessionFactoryBean; } }

    application.properties

    #jdbc jdbc.driver = com.mysql.jdbc.Driver jdbc.url = jdbc:mysql://localhost:3306/ycn_test?useUnicode=true&characterEncoding=utf8&useSSL=false jdbc.username = root jdbc.password = 123456 #Mybatis mybatis_config_file = mybatis-config.xml mapper_path = /mapper/**.xml entity_package = com.imooc.demo.entity

    dao层创建

    public interface AreaDao { List<Area> queryArea(); Area queryAreaById(int areaId); int insertArea(Area area); int updateArea(Area area); int deleteArea(int areaId); }

    mapper编写(dao层类同名xml文件)

    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.imooc.demo.dao.AreaDao"> <select id="queryArea" resultType="com.imooc.demo.entity.Area"> SELECT area_id, area_name, priority, create_time, last_edit_time FROM tb_area ORDER BY priority DESC </select> <select id="queryAreaById" resultType="com.imooc.demo.entity.Area"> SELECT area_id, area_name, priority, create_time, last_edit_time FROM tb_area WHERE area_id = #{areaId} </select> <insert id = "insertArea" useGeneratedKeys = "true" keyProperty = "areaId" keyColumn = "area_id" parameterType = "com.imooc.demo.entity.Area"> INSERT INTO tb_area(area_name, priority, create_time, last_edit_time) VALUES(#{areaName},#{priority},#{createTime},#{lastEditTime}) </insert> <update id = "updateArea" parameterType = "com.imooc.demo.entity.Area"> UPDATE tb_area <set> <if test = "areaName!=null">area_name = #{areaName}</if> <if test = "priority!=null">priority = #{priority}</if> <if test = "lastEditTime!=null">last_edit_time = #{lastEditTime}</if> </set> WHERE area_id = #{areaId} </update> <delete id = "deleteArea"> DELETE FROM tb_area WHERE area_id = #{areaId} </delete> </mapper>

     

    最新回复(0)