├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src └── main ├── java └── sample │ └── mybatis │ ├── DatabaseConfig.java │ ├── MyBatisApplication.java │ ├── domain │ └── User.java │ ├── persistence │ └── UserMapper.java │ └── service │ └── UserService.java └── resources └── schema.sql /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | 3 | *.classpath 4 | 5 | *.project 6 | 7 | *.iml 8 | 9 | *.idea 10 | 11 | .idea/ 12 | 13 | bin/ 14 | 15 | target/ 16 | 17 | .settings/ 18 | 19 | test-output/ 20 | 21 | src/generated/* -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Tim 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | this software and associated documentation files (the "Software"), to deal in 7 | the Software without restriction, including without limitation the rights to 8 | use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | the Software, and to permit persons to whom the Software is furnished to do so, 10 | subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | spring-boot-mybatis-sandbox 2 | =================================== 3 | 4 | Example Spring Boot + MyBatis working with Java Config 5 | 6 | The goal is to show Spring's Config capabilities to work with MyBatis MapperScanner using Java config + Annotations. 7 | 8 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 4.0.0 5 | 6 | org.springframework.boot 7 | spring-boot-starter-parent 8 | 1.2.3.RELEASE 9 | 10 | spring-boot-mybatis-sandbox 11 | jar 12 | 13 | 14 | ${project.groupId} 15 | spring-boot-starter-jdbc 16 | 17 | 18 | org.mybatis 19 | mybatis 20 | 3.2.3 21 | 22 | 23 | org.mybatis 24 | mybatis-spring 25 | 1.2.2 26 | 27 | 28 | com.h2database 29 | h2 30 | 31 | 32 | ${project.groupId} 33 | spring-boot-starter-test 34 | test 35 | 36 | 37 | 38 | 39 | 40 | org.springframework.boot 41 | spring-boot-maven-plugin 42 | 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /src/main/java/sample/mybatis/DatabaseConfig.java: -------------------------------------------------------------------------------- 1 | package sample.mybatis; 2 | 3 | import org.apache.ibatis.session.SqlSessionFactory; 4 | import org.mybatis.spring.SqlSessionFactoryBean; 5 | import org.mybatis.spring.annotation.MapperScan; 6 | import org.springframework.context.annotation.Bean; 7 | import org.springframework.context.annotation.Configuration; 8 | import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder; 9 | 10 | import javax.sql.DataSource; 11 | 12 | import static org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType.H2; 13 | 14 | @Configuration 15 | @MapperScan(basePackages="sample.mybatis.persistence") 16 | public class DatabaseConfig { 17 | 18 | @Bean 19 | public DataSource dataSource() { 20 | return new EmbeddedDatabaseBuilder().setType(H2).build(); 21 | } 22 | 23 | @Bean 24 | public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception { 25 | final SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean(); 26 | sessionFactory.setDataSource(dataSource); 27 | return sessionFactory.getObject(); 28 | } 29 | 30 | } 31 | -------------------------------------------------------------------------------- /src/main/java/sample/mybatis/MyBatisApplication.java: -------------------------------------------------------------------------------- 1 | package sample.mybatis; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.boot.CommandLineRunner; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import sample.mybatis.domain.User; 8 | import sample.mybatis.service.UserService; 9 | 10 | @SpringBootApplication 11 | public class MyBatisApplication implements CommandLineRunner { 12 | 13 | public static void main(String[] args) throws Exception { 14 | SpringApplication.run(MyBatisApplication.class, args); 15 | } 16 | 17 | @Autowired 18 | private UserService userService; 19 | 20 | @Override 21 | public void run(String... args) { 22 | System.out.println("Adding User"); 23 | int userId = userService.addUser(new User("dude@dude.com", "thedude")); 24 | System.out.println("Getting User"); 25 | User user = userService.getUser(userId); 26 | System.out.println("Got User: " + user.getUserName()); 27 | } 28 | 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/sample/mybatis/domain/User.java: -------------------------------------------------------------------------------- 1 | package sample.mybatis.domain; 2 | 3 | public class User { 4 | 5 | private int id; 6 | private String email; 7 | private String userName; 8 | 9 | public User() {} 10 | 11 | public User(String email, String userName) { 12 | this.email = email; 13 | this.userName = userName; 14 | } 15 | 16 | public int getId() { 17 | return id; 18 | } 19 | 20 | public void setId(int id) { 21 | this.id = id; 22 | } 23 | 24 | public String getEmail() { 25 | return email; 26 | } 27 | 28 | public void setEmail(String email) { 29 | this.email = email; 30 | } 31 | 32 | public String getUserName() { 33 | return userName; 34 | } 35 | 36 | public void setUserName(String userName) { 37 | this.userName = userName; 38 | } 39 | 40 | } 41 | -------------------------------------------------------------------------------- /src/main/java/sample/mybatis/persistence/UserMapper.java: -------------------------------------------------------------------------------- 1 | package sample.mybatis.persistence; 2 | 3 | import org.apache.ibatis.annotations.Insert; 4 | import org.apache.ibatis.annotations.Select; 5 | 6 | import sample.mybatis.domain.User; 7 | 8 | public interface UserMapper { 9 | @Select("SELECT * FROM user WHERE id = #{userId}") 10 | public User getUser(long userId); 11 | 12 | @Insert("insert into user (email, userName) values(#{email}, #{userName})") 13 | public int addUser(User user); 14 | } 15 | -------------------------------------------------------------------------------- /src/main/java/sample/mybatis/service/UserService.java: -------------------------------------------------------------------------------- 1 | package sample.mybatis.service; 2 | 3 | import org.springframework.beans.factory.annotation.Autowired; 4 | import org.springframework.stereotype.Service; 5 | import sample.mybatis.domain.User; 6 | import sample.mybatis.persistence.UserMapper; 7 | 8 | @Service 9 | public class UserService { 10 | 11 | @Autowired 12 | private UserMapper userMapper; 13 | 14 | public int addUser(User userToAdd) { 15 | return userMapper.addUser(userToAdd); 16 | } 17 | 18 | public User getUser(int userId) { 19 | return userMapper.getUser(userId); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/resources/schema.sql: -------------------------------------------------------------------------------- 1 | create table IF NOT EXISTS user ( 2 | id int primary key auto_increment, 3 | email varchar(255), 4 | userName varchar(255) 5 | ); --------------------------------------------------------------------------------