Spring boot注解开发mybatis

数据库连接配置,在resources目录下的application.propertis文件配置:

#数据库连接配置
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db1
spring.datasource.username=root
spring.datasource.password=qw123456
#指定mybatis日志的输出位置,输出控制台
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl

在sql接口使用注解@Mapper。作用就是将接口生成一个动态代理类。这个注解就是用来映射mapper.xml文件的。

如果mapper接口方法形参只有一个普通类型的参数,#{...}里面的属性名可以随便写。

参数占位符:

        #{...}:执行sql语句时,会将#{...}替换为?,生成预编译sql,会自动设置参数值。使用时机:参数传递,都使用#{...}。

                预编译sql:性能更高,更加安全(防止sql注入)

        ${...}:拼接sql。直接将参数拼接在sql语句中,存在sql注入问题。使用时机:如果对表名、列表进行动态设置时使用。

数据封装:

        mybatis要求列名与属性名一直才能自动封装数据,若列名与属性名不一致,则需要手动映射。

       1、起别名:在sql语句中,对不一样的列名起别名,别名和实体类属性名一样。

@Select("select id, username, password, name, gender, image, job, entrydate," +
            " dept_id deptId, create_time createTime, update_time updateTime " +
            "from emp where id=#{id}")
public Emp selectById(int id);

        2、手动结果映射:通过@Results及@Result进行手动结果映射。

@Select("select * from emp where id=#{id}")
@Results({
        @Result(column="dept_id",property="deptId"),
        @Result(column = "create_time",property="createTime"),
        @Result(column = "update_time",property="updateTime")
})
public Emp selectById(int id);

        3、对于字段名与属性名符合驼峰命名规则,mybatis会自动通过驼峰命名规则映射。

#开启驼峰命名自动映射
mybatis.configuration.map-underscore-to-camel-case=true
@Mapper
public interface EmpMapper {
    @Delete("delete from emp where id=#{id}")
    public int deleteForId(int id);

//    将自动生成的主键值返回,赋值给emp的id对象
    @Options(keyProperty = "id",useGeneratedKeys = true)
    @Insert("insert into emp(username, password, name, gender, image, job, entrydate, dept_id, create_time, update_time) VALUES" +
            "(#{username},#{password},#{name}, #{gender}, #{image}, #{job}, #{entrydate}, #{deptId}, #{createTime}, #{updateTime})")
    public int insert(Emp emp);
}

在一个字段内传递参数时可使用concat来进行连接。比如:'%#{name}%'是不可用的,进行预编译时会将替换的?当成字符串来处理,无法传参,可使用concat('%',#{name},'%')来将字符串进行拼接。

  • 5
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Spring Boot Starter 是 Spring Boot 应用程序的预配置模块,可以帮助我们快速集成常用框架。MyBatis 是一个持久层框架,用于映射 Java 对象到数据库表。 如果要在 Spring Boot 应用程序中使用 MyBatis,可以创建自定义的 Spring Boot Starter 来简化配置。 步骤如下: 1. 创建一个 maven 项目,并在 pom 文件中添加依赖 spring-boot-starter、mybatis-spring-boot-starter。 2. 创建一个配置类,来配置 MyBatis。 3. 创建一个自动配置类,用于自动配置 MyBatis。 4. 创建一个 starter 类,用于向 Spring Boot 提供自动配置。 5. 在 pom 文件中添加相关信息,用于发布到 maven 仓库。 6. 发布到 maven 仓库,并在其他项目中使用。 如果你想要详细了解,可以参考官网上关于 Spring Boot Starter 的文档。 ### 回答2: 要自定义 Spring Boot Starter 操作 MyBatis 数据库,可以按照以下步骤进行操作: 1. 创建一个 Maven 项目,并指定父项目为 Spring Boot Starter Parent。 2. 在项目的 pom.xml 文件中添加必要的依赖,包括 Spring Boot Starter、MyBatis 和相应的数据库驱动程序。 3. 创建一个自定义的配置类,用于配置 MyBatis 的数据源、事务管理器等。 4. 在配置类中使用 @Configuration 注解标注该类为配置类,并使用 @EnableConfigurationProperties 注解引入配置属性。 5. 创建一个自定义的 Starter 类,用于自动配置 MyBatis 相关的组件。 6. 在 Starter 类中使用 @Configuration 注解标注该类为配置类,并使用 @EnableAutoConfiguration 注解启用自动配置。 7. 在 Starter 类中使用 @ConditionalOnClass 注解,指定条件,在类路径下存在 MyBatis 相关的类时才进行自动配置。 8. 在 Starter 类中使用 @Import 注解,导入配置类,将自定义的配置应用到 Spring Boot 项目中。 9. 编写自定义的配置文件,用于配置 MyBatis 的相关属性,例如数据库连接信息、Mapper 扫描路径等。 10. 在项目的 pom.xml 文件中添加 spring.factories 文件,将自定义的 Starter 类注册到 Spring Boot 应用中。 11. 在 Spring Boot 项目中添加对自定义 Starter 的依赖,可通过 Maven 依赖坐标来引入。 12. 配置项目的 application.properties 或 application.yml 文件,指定数据库相关的信息以及其他自定义属性。 经过以上步骤的操作,就可以自定义 Spring Boot Starter 来操作 MyBatis 数据库了。可以通过引入自定义的 Starter 来简化项目的配置,并在应用中直接使用 MyBatis 进行数据库操作,提高开发效率和代码的可维护性。 ### 回答3: 自定义Spring Boot Starter操作Mybatis数据库涉及以下步骤: 1. 创建一个新的Maven项目,并在pom.xml文件中添加Spring BootMybatis依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> ``` 2. 创建一个自定义的starter模块,在其中定义配置类和Mybatis相关配置: ```java @Configuration @AutoConfigureAfter(DataSourceAutoConfiguration.class) @EnableConfigurationProperties(MybatisProperties.class) public class MybatisAutoConfiguration { @Autowired private MybatisProperties properties; @Autowired(required = false) private List<Interceptor> interceptors; @Bean public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { SqlSessionFactoryBean factory = new SqlSessionFactoryBean(); factory.setDataSource(dataSource); factory.setConfiguration(properties.getConfiguration()); if (interceptors != null) { factory.setPlugins(interceptors.toArray(new Interceptor[0])); } return factory.getObject(); } @Bean public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) { return new SqlSessionTemplate(sqlSessionFactory); } } ``` 3. 创建自定义的配置类MybatisProperties,用于定义数据库的相关配置: ```java @ConfigurationProperties(prefix = "spring.mybatis") public class MybatisProperties { private Configuration configuration; // 其他配置... // Getter和Setter方法 } ``` 4. 在resources目录下创建配置文件application.yml,配置数据库相关信息: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/mydb username: username password: password spring: mybatis: configuration: map-underscore-to-camel-case: true cache-enabled: true ``` 5. 在使用的项目中添加自定义的starter依赖,并在application类上添加@EnableMybatis注解: ```java @SpringBootApplication @EnableMybatis public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 通过以上步骤,我们就可以自定义Spring Boot Starter来操作Mybatis数据库了。这样做的好处是,可以将Mybatis的配置和操作封装在starter中,使得项目更加简洁,并且能够方便地重用该starter。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值