├── .gitignore ├── LICENSE ├── README.md ├── pom.xml └── src └── main ├── java └── com │ └── llf │ └── springboot │ ├── Application.java │ ├── controller │ └── HelloController.java │ ├── dao │ └── UserDao.java │ ├── model │ └── User.java │ ├── service │ ├── UserService.java │ └── impl │ │ └── UserServiceImpl.java │ └── test │ └── TestUser.java ├── resources ├── application.properties ├── application.yml └── mapper │ └── UserMapper.xml └── webapp ├── WEB-INF └── web.xml └── index.jsp /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | target/ 3 | SpringBoot_Demo.iml 4 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2016 Oliver Lee 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, 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, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # SpringBoot_Demo 2 | SpringBoot+Mybatis+MySQL 3 | 4 | --- 5 | 6 | 前段时间去了解了SpringBoot,感觉这东西太方便,不去要部署到Tomcat,也不需要配置文件,直接写一句代码就跑起来一个web项目,直接打包成jar运行就ok了。大型项目会不会使用SpringBoot我不太清除,但对于我这种入门程序员想做一些小项目简直太方便。 7 | 8 | 入门SpringBoot非常简单,Spring官网有教程,如果学过Spring和SpringMVC,那么简单5分钟即可入门。当然,如果没有学习过Spring和SpringMVC也不推荐去学习SpringBoot,建议先将前边两个至少入了门,毕竟这俩才是核心。 9 | 10 | 此项目是Maven项目,IDE为Intellij IDEA 11 | 12 | ## 依赖 13 | 14 | 国际惯例,先上依赖。 15 | 16 | 这是SpringBoot文档给出的依赖,只需要以下一个jar包,SpringBoot就能跑起来。 17 | 18 | 19 | org.springframework.boot 20 | spring-boot-starter-parent 21 | 1.4.1.RELEASE 22 | 23 | 24 | 25 | 26 | 27 | org.springframework.boot 28 | spring-boot-starter-web 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | org.springframework.boot 37 | spring-boot-maven-plugin 38 | 39 | 40 | 41 | 42 | 其余jar包 43 | 44 | 加入Junit依赖 45 | 46 | 47 | junit 48 | junit 49 | 4.12 50 | 51 | 52 | 开发工具,自动检测classpath变化并重新部署 53 | 54 | 55 | org.springframework.boot 56 | spring-boot-devtools 57 | true 58 | 59 | 60 | SpringBoot测试框架 61 | 62 | 63 | org.springframework.boot 64 | spring-boot-starter-test 65 | 66 | 67 | Mybatis-SpringBoot集成,SpringBoot官方只集成了JPA,并没有集成Mybatis,但Mybatis出了与SpringBoot的集成jar包 68 | 69 | 70 | org.mybatis.spring.boot 71 | mybatis-spring-boot-starter 72 | 1.1.1 73 | 74 | 75 | MySQL驱动jar包 76 | 77 | 78 | mysql 79 | mysql-connector-java 80 | 5.1.38 81 | 82 | 83 | druid,alibaba开源的数据库连接池,可以抛弃dbcp和c3p0了,毕竟好久没更新了,而且alibaba在大数据和高并发上有丰富的经验,性能是非常不错的。如fastjson也有越来越多的开发者使用。 84 | 85 | 86 | com.alibaba 87 | druid 88 | 1.0.26 89 | 90 | 91 | --- 92 | 93 | ## 配置 94 | 95 | 配置数据库信息和连接池信息,我是使用yml文件来配置,更加简洁 96 | 97 | 在resources下新建application.yml文件,写入以下配置。 98 | 99 | **注意:**具体配置项,冒号后面要有空格!如'name: mydb',但如'spring:'则不需要。 100 | 101 | #数据库相关配置 102 | spring: 103 | datasource: 104 | name: mydb 105 | type: com.alibaba.druid.pool.DruidDataSource 106 | url: jdbc:mysql://127.0.0.1:3306/phycholee_blog?useUnicode=true&characterEncoding=UTF-8 107 | username: root 108 | password: root 109 | driver-class-name: com.mysql.jdbc.Driver 110 | minIdle: 1 111 | maxActive: 20 112 | initialSize: 1 113 | timeBetweenEvictionRunsMillis: 3000 114 | minEvictableIdleTimeMillis: 300000 115 | 116 | #Mabatis的Mapper的XML目录,(可选,详情下面讲) 117 | mybatis: 118 | mapperLocations: classpath*:mapper/*.xml 119 | typeAliasesPackage: com.llf.springboot.model 120 | 121 | 还可以用properties文件配置,如果yml和properties都存在,SpringBoot默认会**选择properties**。 122 | 123 | spring.datasource.url=jdbc:MySQL://127.0.0.1:3306/phycholee_blog 124 | spring.datasource.username=root 125 | spring.datasource.password=root 126 | spring.datasource.driver-class-name=com.mysql.jdbc.Driver 127 | spring.datasource.max-idle=5 128 | spring.datasource.max-wait=10000 129 | spring.datasource.min-idle=1 130 | spring.datasource.initial-size=3 131 | 132 | server.port=8088 133 | server.session.timeout=10 134 | server.tomcat.max-threads=800 135 | server.tomcat.uri-encoding=UTF-8 136 | 137 | mybatis.mapperLocations=classpath*:mapper/*.xml 138 | mybatis.typeAliasesPackage=com.llf.springboot.model 139 | 140 | --- 141 | 142 | ## SpringBoot入口类 143 | 144 | 在项目目录下,创建Application.java类,代码如下 145 | 146 | @SpringBootApplication 147 | @EnableTransactionManagement //开启事务注解 148 | @MapperScan("com.llf.springboot.dao") //扫描Mybatis Mapper接口 149 | public class Application { 150 | public static void main(String[] args) throws Exception { 151 | SpringApplication.run(Application.class, args); 152 | } 153 | } 154 | 155 | 其中@SpringBootApplication为最主要的注解,它等价于以下三个注解 156 | 157 | @Configuration //代替XML配置文件 158 | @EnableAutoConfiguration //自动配置其他bean,比如Mybatis的SQLSessionFactoryBean 159 | @ComponentScan //扫描注解 160 | 161 | --- 162 | 163 | ## Mybatis Mapper 164 | 165 | ### Mapper XML 166 | 167 | 首先Mapper.xml文件,这是操作数据库的关键配置。mapper放在resources文件夹下,在上面yml配置的'mapperLocations'就是配置此路径。 168 | 169 | **注意:**此文件不要放在java文件目录如'controller','service'的父目录下,不然Spring会扫描不到。 170 | 171 | ### Mapper接口 172 | 173 | //@Mapper 174 | public interface UserDao { 175 | 176 | // @Select("select * from user where id = #{id}") 177 | User findById(Integer id) throws SQLException; 178 | } 179 | 180 | 上面'@Mapper'注掉是因为在入口类Application.java中有注解@MapperScan("com.llf.springboot.dao"),Spring会自动在此文件夹下扫描。(这里使用dao是因为个人习惯,官方是使用mapper) 181 | 182 | 如果不想使用xml文件写sql,可以使用 @Select("")直接在mapper接口里写sql。 183 | 184 | @Select("") 185 | @Insert("") 186 | @Delete("") 187 | @Update("") 188 | 189 | ## Junit 190 | 191 | SpringBoot中使用Junit,只需在Test类的类名上添加注解,需要加入'spring-boot-starter-test' jar包 192 | 193 | @RunWith(SpringJUnit4ClassRunner.class) 194 | @SpringApplicationConfiguration(classes = Application.class) 195 | @WebAppConfiguration 196 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | com.llf 5 | springboot 6 | war 7 | 1.0-SNAPSHOT 8 | springboot Maven Webapp 9 | http://maven.apache.org 10 | 11 | 12 | org.springframework.boot 13 | spring-boot-starter-parent 14 | 1.4.1.RELEASE 15 | 16 | 17 | 18 | 19 | junit 20 | junit 21 | 4.12 22 | 23 | 24 | 25 | org.springframework.boot 26 | spring-boot-starter-web 27 | 28 | 29 | 30 | 31 | org.springframework.boot 32 | spring-boot-devtools 33 | true 34 | 35 | 36 | 37 | 38 | org.springframework.boot 39 | spring-boot-starter-test 40 | 41 | 42 | 43 | 44 | org.mybatis.spring.boot 45 | mybatis-spring-boot-starter 46 | 1.1.1 47 | 48 | 49 | 50 | 51 | mysql 52 | mysql-connector-java 53 | 5.1.38 54 | 55 | 56 | 57 | 58 | com.alibaba 59 | druid 60 | 1.0.26 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | org.springframework.boot 69 | spring-boot-maven-plugin 70 | 71 | 72 | 73 | springboot 74 | 75 | 76 | -------------------------------------------------------------------------------- /src/main/java/com/llf/springboot/Application.java: -------------------------------------------------------------------------------- 1 | package com.llf.springboot; 2 | 3 | 4 | import org.mybatis.spring.annotation.MapperScan; 5 | import org.springframework.boot.SpringApplication; 6 | import org.springframework.boot.autoconfigure.SpringBootApplication; 7 | import org.springframework.transaction.annotation.EnableTransactionManagement; 8 | 9 | /** 10 | * Created by Lee on 2016/10/13. 11 | */ 12 | @SpringBootApplication 13 | @EnableTransactionManagement //开启事务注解 14 | @MapperScan("com.llf.springboot.dao") 15 | public class Application { 16 | public static void main(String[] args) throws Exception { 17 | SpringApplication.run(Application.class, args); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/llf/springboot/controller/HelloController.java: -------------------------------------------------------------------------------- 1 | package com.llf.springboot.controller; 2 | 3 | import com.llf.springboot.model.User; 4 | import com.llf.springboot.service.UserService; 5 | import org.springframework.beans.factory.annotation.Autowired; 6 | import org.springframework.web.bind.annotation.RequestMapping; 7 | import org.springframework.web.bind.annotation.RestController; 8 | 9 | import java.sql.SQLException; 10 | 11 | /** 12 | * Created by PhychoLee on 2016/10/13. 13 | * Description: 14 | */ 15 | @RestController 16 | public class HelloController { 17 | @Autowired 18 | private UserService userService; 19 | 20 | @RequestMapping("/") 21 | public User getUser(){ 22 | User user =null; 23 | try { 24 | user = userService.findById(1); 25 | } catch (SQLException e) { 26 | e.printStackTrace(); 27 | } 28 | return user; 29 | } 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /src/main/java/com/llf/springboot/dao/UserDao.java: -------------------------------------------------------------------------------- 1 | package com.llf.springboot.dao; 2 | 3 | 4 | import com.llf.springboot.model.User; 5 | 6 | import java.sql.SQLException; 7 | 8 | public interface UserDao { 9 | 10 | // @Select("select * from user where id = #{id}") 11 | User findById(Integer id) throws SQLException; 12 | 13 | void insert(User user) throws SQLException; 14 | } -------------------------------------------------------------------------------- /src/main/java/com/llf/springboot/model/User.java: -------------------------------------------------------------------------------- 1 | package com.llf.springboot.model; 2 | 3 | public class User { 4 | 5 | private Integer id; 6 | 7 | private String name; 8 | 9 | public Integer getId() { 10 | return id; 11 | } 12 | 13 | public void setId(Integer id) { 14 | this.id = id; 15 | } 16 | 17 | public String getName() { 18 | return name; 19 | } 20 | 21 | public void setName(String name) { 22 | this.name = name == null ? null : name.trim(); 23 | } 24 | 25 | @Override 26 | public String toString() { 27 | return "User{" + 28 | "id=" + id + 29 | ", name='" + name + '\'' + 30 | '}'; 31 | } 32 | } -------------------------------------------------------------------------------- /src/main/java/com/llf/springboot/service/UserService.java: -------------------------------------------------------------------------------- 1 | package com.llf.springboot.service; 2 | 3 | import com.llf.springboot.model.User; 4 | 5 | import java.sql.SQLException; 6 | import java.util.List; 7 | 8 | /** 9 | * Created by PhychoLee on 2016/11/4 17:15. 10 | * Description: 用户Service接口 11 | */ 12 | public interface UserService { 13 | 14 | User findById(Integer id) throws SQLException; 15 | 16 | void insert(User user) throws SQLException; 17 | 18 | void insertList(List userList) throws SQLException; 19 | } 20 | -------------------------------------------------------------------------------- /src/main/java/com/llf/springboot/service/impl/UserServiceImpl.java: -------------------------------------------------------------------------------- 1 | package com.llf.springboot.service.impl; 2 | 3 | import com.llf.springboot.dao.UserDao; 4 | import com.llf.springboot.model.User; 5 | import com.llf.springboot.service.UserService; 6 | import org.springframework.beans.factory.annotation.Autowired; 7 | import org.springframework.stereotype.Service; 8 | import org.springframework.transaction.annotation.Transactional; 9 | 10 | import java.sql.SQLException; 11 | import java.util.List; 12 | 13 | /** 14 | * Created by PhychoLee on 2016/11/4 17:18. 15 | * Description: 用户service实现类 16 | */ 17 | @Service 18 | public class UserServiceImpl implements UserService { 19 | 20 | @Autowired 21 | private UserDao userDao; 22 | 23 | @Override 24 | public User findById(Integer id) throws SQLException { 25 | return userDao.findById(id); 26 | } 27 | 28 | @Override 29 | public void insert(User user) throws SQLException { 30 | userDao.insert(user); 31 | } 32 | 33 | /** 34 | * 测试事务 35 | * @param userList 36 | * @throws SQLException 37 | */ 38 | @Transactional 39 | public void insertList(List userList) throws SQLException { 40 | insert(userList.get(0)); 41 | int i = 1/0; 42 | insert(userList.get(1)); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/com/llf/springboot/test/TestUser.java: -------------------------------------------------------------------------------- 1 | package com.llf.springboot.test; 2 | 3 | import com.llf.springboot.Application; 4 | import com.llf.springboot.model.User; 5 | import com.llf.springboot.service.UserService; 6 | import org.junit.Test; 7 | import org.junit.runner.RunWith; 8 | import org.springframework.beans.factory.annotation.Autowired; 9 | import org.springframework.boot.test.SpringApplicationConfiguration; 10 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 11 | import org.springframework.test.context.web.WebAppConfiguration; 12 | 13 | import java.sql.SQLException; 14 | import java.util.ArrayList; 15 | import java.util.List; 16 | 17 | /** 18 | * Created by PhychoLee on 2016/11/4 10:49. 19 | * Description: 20 | */ 21 | @RunWith(SpringJUnit4ClassRunner.class) 22 | @SpringApplicationConfiguration(classes = Application.class) 23 | @WebAppConfiguration 24 | public class TestUser { 25 | @Autowired 26 | private UserService userService; 27 | 28 | @Test 29 | public void testFind() throws SQLException { 30 | User user = userService.findById(1); 31 | System.out.println(user); 32 | } 33 | 34 | @Test 35 | public void testInsert() throws SQLException { 36 | User user = new User(); 37 | user.setName("AA"); 38 | 39 | User user1 = new User(); 40 | user1.setName("BB"); 41 | 42 | List userList = new ArrayList(); 43 | userList.add(user); 44 | userList.add(user1); 45 | 46 | userService.insertList(userList); 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /src/main/resources/application.properties: -------------------------------------------------------------------------------- 1 | #spring.datasource.url=jdbc:MySQL://127.0.0.1:3306/phycholee_blog 2 | #spring.datasource.username=root 3 | #spring.datasource.password=root 4 | #spring.datasource.driver-class-name=com.mysql.jdbc.Driver 5 | #spring.datasource.max-idle=5 6 | #spring.datasource.max-wait=10000 7 | #spring.datasource.min-idle=1 8 | #spring.datasource.initial-size=3 9 | # 10 | #server.port=8088 11 | #server.session.timeout=10 12 | #server.tomcat.max-threads=800 13 | #server.tomcat.uri-encoding=UTF-8 14 | # 15 | #mybatis.mapperLocations=classpath*:mapper/*.xml 16 | #mybatis.typeAliasesPackage=com.llf.springboot.model -------------------------------------------------------------------------------- /src/main/resources/application.yml: -------------------------------------------------------------------------------- 1 | spring: 2 | datasource: 3 | name: mydb 4 | type: com.alibaba.druid.pool.DruidDataSource 5 | url: jdbc:mysql://127.0.0.1:3306/phycholee_blog?useUnicode=true&characterEncoding=UTF-8 6 | username: root 7 | password: root 8 | driver-class-name: com.mysql.jdbc.Driver 9 | minIdle: 1 10 | maxActive: 20 11 | initialSize: 1 12 | timeBetweenEvictionRunsMillis: 3000 13 | minEvictableIdleTimeMillis: 300000 14 | # validationQuery: SELECT 'ZTM' FROM DUAL 15 | # testWhileIdle: true 16 | # testOnBorrow: false 17 | # testOnReturn: false 18 | 19 | 20 | mybatis: 21 | mapperLocations: classpath*:mapper/*.xml 22 | typeAliasesPackage: com.llf.springboot.model -------------------------------------------------------------------------------- /src/main/resources/mapper/UserMapper.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | id, name 10 | 11 | 12 | 18 | 19 | 20 | insert into user (name) 21 | values (#{name,jdbcType=VARCHAR}) 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/main/webapp/WEB-INF/web.xml: -------------------------------------------------------------------------------- 1 | 4 | 5 | 6 | Archetype Created Web Application 7 | 8 | -------------------------------------------------------------------------------- /src/main/webapp/index.jsp: -------------------------------------------------------------------------------- 1 | 2 | 3 |

Hello World!

4 | 5 | 6 | --------------------------------------------------------------------------------