一、java web开发环境搭建
网上有很多教程,参考教程:http://www.cnblogs.com/Leo_wl/p/4752875.html
二、Spring boot搭建
1、Intellij idea菜单栏File->new->project。
2、选择左侧栏中spring initializr,右侧选择jdk版本,以及默认的Service URL,点击next。
/3、然后填写项目的Group、Artifact等信息,helloworld阶段选默认就可以了,点击next。
4、左侧点击Web,中间一侧选择Web,然后左侧选择SQL,中间一侧选择JPA、Mybatis、MYSQL(LZ数据库用的是mysql,大家可以选择其他DB),点击next。
5、填写Project name 等信息,然后点击Finish。
至此,一个maven web项目就创建好了,目录结构如下:
这样,Spring boot就搭建好了,pom.xml里已经有了Spring boot的jar包,包括我们的mysql数据连接的jar包。Spring boot内置了类似tomcat这样的中间件,所以,只要运行DemoApplication中的main方法就可以启动项目了。我们测试一下。
在src/main/java下新建目录com/demo/entity/User。
[](javascript:void(0); "复制代码")
package com.demo.entity; public class User { private String name; public String getName() { return name;[
} public void setName(String name) { this.name = name;
}
}
](javascript:void(0); "复制代码")
相同目录下新建com/demo/controller/TestBootController。
[](javascript:void(0); "复制代码")
package com.demo.controller; import com.demo.entity.User; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;[@RestController
@EnableAutoConfiguration
@RequestMapping("/testboot") public class TestBootController {
@RequestMapping("getuser") public User getUser() {
User user = new User();
user.setName("test"); return user;
}
}
](javascript:void(0); "复制代码")
spring boot启动DemoAplication是需要扫描它下面的Controller等类的,所以将DemoApplication移动到com/demo目录下。还有就是Spring boot启动默认是要加载数据源的,所以我们在src/main/resources下新建application.yml:
按 Ctrl+C 复制代码
按 Ctrl+C 复制代码
或者将pom.xml中加载数据源的jar包先注释掉也可以。
//
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.3.0
最终的目录结构如下,
启动DemoApplication的main方法,访问http://localhost:8080/testboot/getuser即可。
三、整合Mybatis
1、集成druid,使用连接池。pom.xml中添加:
com.alibaba
druid
1.1.0
最终的pom.xml文件:
[](javascript:void(0); "复制代码")
[
4.0.0
com.arm demo 0.0.1-SNAPSHOT jar demo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.5.8.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-data-jpa org.mybatis.spring.boot mybatis-spring-boot-starter 1.3.1 org.springframework.boot spring-boot-starter-web mysql mysql-connector-java runtime org.springframework.boot spring-boot-starter-test test com.alibaba druid 1.1.0 org.springframework.boot spring-boot-maven-plugin
](javascript:void(0); "复制代码")
在application.yml中添加数据源、Mybatis的实体和配置文件位置。
[](javascript:void(0); "复制代码")
#默认使用配置[
spring:
profiles:
active: dev公共配置与profiles选择无关 mapperLocations指的路径是src/main/resources
mybatis:
typeAliasesPackage: com.xdd.entity
mapperLocations: classpath:mapper/*.xml
开发配置
spring:
profiles: devdatasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
](javascript:void(0); "复制代码")
就这样就整合完成了!我们测试一下。
用MyBatis Generator自动生成代码,参考博文:http://blog.csdn.net/zhshulin/article/details/23912615 这里列一下自动生成的代码。
[](javascript:void(0); "复制代码")
import com.xdd.entity.User; import org.springframework.stereotype.Component; public interface UserDao { int deleteByPrimaryKey(Integer id); int insert(User record); int insertSelective(User record);[User selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(User record); int updateByPrimaryKey(User record);
}
](javascript:void(0); "复制代码")
UserMapper.xml
[](javascript:void(0); "复制代码")
[
id, user_name, password, age
delete from user_t
where id = #{id,jdbcType=INTEGER}
insert into user_t (id, user_name, password,
age)
values (#{id,jdbcType=INTEGER}, #{userName,jdbcType=VARCHAR}, #{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER})
insert into user_t
id,
user_name,
password,
age,
#{id,jdbcType=INTEGER},
#{userName,jdbcType=VARCHAR},
#{password,jdbcType=VARCHAR},
#{age,jdbcType=INTEGER},
update user_t where id = #{id,jdbcType=INTEGER}
user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER},
update user_t
set user_name = #{userName,jdbcType=VARCHAR},
password = #{password,jdbcType=VARCHAR},
age = #{age,jdbcType=INTEGER}
where id = #{id,jdbcType=INTEGER}
](javascript:void(0); "复制代码")
[](javascript:void(0); "复制代码")
public class User { private Integer id; private String userName; private String password; private Integer age; public Integer getId() { return id;[
} public void setId(Integer id) { this.id = id;
} public String getUserName() { return userName;
} public void setUserName(String userName) { this.userName = userName == null ? null : userName.trim();
} public String getPassword() { return password;
} public void setPassword(String password) { this.password = password == null ? null : password.trim();
} public Integer getAge() { return age;
} public void setAge(Integer age) { this.age = age;
}
}
](javascript:void(0); "复制代码")
最后将DemoApplication.java修改一下,让其扫描dao层接口。
[](javascript:void(0); "复制代码")
import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.web.support.SpringBootServletInitializer;[@SpringBootApplication
@MapperScan("com.xdd.dao") public class DemoApplication extends SpringBootServletInitializer{ public static void main(String[] args) {
SpringApplication.run(DemoApplication.class,args);
}
}
](javascript:void(0); "复制代码")
自己添加controller和service
[](javascript:void(0); "复制代码")
import java.util.List; import java.util.Map; public interface UserService { public User getUserById(int userId); boolean addUser(User record);[}
](javascript:void(0); "复制代码")
[](javascript:void(0); "复制代码")
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.List; import java.util.Map;[@Service("userService") public class UserServiceImpl implements UserService {
@Resource private UserDao userDao; public User getUserById(int userId) { return userDao.selectByPrimaryKey(userId); } public boolean addUser(User record){ boolean result = false; try { userDao.insertSelective(record); result = true; } catch (Exception e) { e.printStackTrace(); } return result; }
}
](javascript:void(0); "复制代码")
[](javascript:void(0); "复制代码")
@Controller[
@RequestMapping("/user") public class UserController {
@Resource private UserService userService;@RequestMapping("/showUser") @ResponseBody public User toIndex(HttpServletRequest request, Model model){ int userId = Integer.parseInt(request.getParameter("id")); User user = this.userService.getUserById(userId); return user; }
}
](javascript:void(0); "复制代码")
浏览器访问http://localhost:8080/user/showUser?id=1