两种配置方式

  • xml配置
  • 注解配置

    xml配置

    自查

注解配置

配置pom.xml

  1. <dependency>
  2. <groupId>org.mybatis.spring.boot</groupId>
  3. <artifactId>mybatis-spring-boot-starter</artifactId>
  4. <version>2.2.1</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>mysql</groupId>
  8. <artifactId>mysql-connector-java</artifactId>
  9. <scope>runtime</scope>
  10. </dependency>

创建application.yml文件

  1. server:
  2. port: 8080
  3. servlet:
  4. context-path: /
  5. spring:
  6. datasource:
  7. driver-class-name: com.mysql.jdbc.Driver
  8. username: root
  9. password: root
  10. url: jdbc:mysql://localhost:3306/study?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai

测试能否链接mysql

  1. @SpringBootApplication
  2. @MapperScan("com.wg.demo.mapper")
  3. public class EasyexcelApplication {
  4. public static void main(String[] args) {
  5. ConfigurableApplicationContext context = SpringApplication.run(DemoApplication.class, args);
  6. JdbcTemplate jdbcTemplate = context.getBean(JdbcTemplate.class);
  7. List<Map<String, Object>> result = jdbcTemplate.queryForList("SELECT * FROM member");
  8. System.out.println(result);
  9. }
  10. }

测试成功后
启动类上添加@MapperScan("com.wg.demo.mapper")

  1. @SpringBootApplication
  2. @MapperScan("com.wg.demo.mapper")
  3. public class EasyexcelApplication {
  4. public static void main(String[] args) {
  5. SpringApplication.run(DemoApplication.class, args);
  6. }
  7. }

根据com.wg.demo.mapper路径,创建mapper
创建MemberMapper
若启动类上,添加了@MapperScan注解,则具体的mapper类上不用再添加@Mapper注解

  1. @Mapper
  2. public interface MemberMapper {
  3. @Select("SELECT * FROM member")
  4. @Results({
  5. @Result(property = "id", column = "id"),
  6. @Result(property = "username", column = "username"),
  7. @Result(property = "password", column = "password"),
  8. @Result(property = "nickname", column = "nickname"),
  9. @Result(property = "birthday", column = "birthday"),
  10. @Result(property = "phone", column = "phone"),
  11. @Result(property = "gender", column = "gender"),
  12. })
  13. List<Member> getMember();
  14. @Options(useGeneratedKeys = true, keyProperty = "po.id", keyColumn = "id")
  15. @Insert("insert into member (username, password, nickname, birthday, phone, gender) values (#{po.username}, #{po.password}, #{po.nickname}, #{po.birthday}, #{po.phone}, #{po.gender})")
  16. int save(@Param("po") MemberPo po);
  17. @Update("update member set `username`=#{username} where id = #{id}")
  18. int add(@Param("id") int id, @Param("username") String username);
  19. @Delete("delete from member where id = #{id}")
  20. int delete(@Param("id") int id);
  21. }

@Insert注解用来插入
@Options注解用来指定一些配置信息,比如上面的语句中用来配置将插入的id,保存到参数MemberPo的id字段上

在service类中,通过@Autowired注入来使用

  1. @Service
  2. public class MemberServiceImpl {
  3. @Autowired
  4. MemberMapper mapper;
  5. public List<Member> getMember(){
  6. return mapper.getMember();
  7. }
  8. }

over

稍微复杂高级点的操作

  1. /**
  2. * foreach 查询
  3. *
  4. * @param ids
  5. * @return
  6. */
  7. @Select("<script> select * from member where id in " +
  8. "<foreach collection='ids' index='index' item='id' open='(' separator=',' close=')'>" +
  9. "#{id}" +
  10. "</foreach></script>")
  11. List<MemberPo> getByIds(@Param("ids") List<Integer> ids);

公众号:程序员WeiG
个人博客:wggz.top