springboot 零xml集成mybatis
阿新 • • 發佈:2017-12-25
style ans https cti ddr www package sys use
maven依賴
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>springboot-demo</artifactId> <groupId>cn.xiaojf</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>springboot-mybatis-annotation</artifactId> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency> </dependencies> </project>
application.properties
#應用端口號 server.port=8010 #freemarker 默認文件後綴 spring.freemarker.suffix=.html #數據庫設置 spring.datasource.driverClassName = com.mysql.jdbc.Driver spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8 spring.datasource.username = root spring.datasource.password = root #mybatis 設置 ##mybatis.type-aliases-package=cn.xiaojf.springboot.mybatis.annotation.entity mybatis.mapper-locations=classpath:**/mapper/*.xml
Application.java
package cn.xiaojf.springboot.mybatis.annotation;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Mapper
package cn.xiaojf.springboot.mybatis.annotation.mapper; import cn.xiaojf.springboot.mybatis.annotation.entity.User; import org.apache.ibatis.annotations.Mapper; import java.util.List; @Mapper public interface UserMapper { List<User> findByUserName(String name); User findUserAddrByName(String name); }
在service中的使用
UserService.java
package cn.xiaojf.springboot.mybatis.annotation.service; public interface UserService { void findByUserName(String name); void findUserAddrByName(String name); }
UserServiceImpl.java
package cn.xiaojf.springboot.mybatis.annotation.service.impl; import cn.xiaojf.springboot.mybatis.annotation.entity.User; import cn.xiaojf.springboot.mybatis.annotation.entity.UserAddr; import cn.xiaojf.springboot.mybatis.annotation.mapper.UserMapper; import cn.xiaojf.springboot.mybatis.annotation.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public void findByUserName(String name) { List<User> list = userMapper.findByUserName(name); if (list != null) { for (User user : list) { System.out.println(user.getName()); } } } @Override public void findUserAddrByName(String name) { User user = userMapper.findUserAddrByName(name); if (user != null) { List<UserAddr> userAddrList = user.getUserAddrList(); if (userAddrList != null) { for (UserAddr userAddr : userAddrList) { System.out.println(userAddr.getName()); } } } } }
源碼地址
https://gitee.com/xiaojf/springboot-demo/tree/master/springboot-mybatis-annotation
springboot 零xml集成mybatis