1、
本人也是学习springboot不久,其良好的约定简化了配置,让我爱不释手,相比于jfinal,springboot感觉更好一点,当然这是本人意见,不喜勿喷。我在学习中也遇到了各种问题,也查找了各种资料,在这里就给大家说一下,希望大牛口下留情。
2、
首先新建一个maven项目,我采用的是eclipse,安装了springsource-tool-suite,如果你电脑没安装也不影响,只需要建立相应的目录就行,如果想安装可以参考这篇文章http://blog.csdn.net/two_people/article/details/70242206
3、
新建好项目 目录结构如下
当然我的已经是完整的了,你的目录参照这个,然后我们一步一步的填充
4、
(1)新建实体类,代码如下
package com.qbd.model;
import java.io.Serializable;
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
private int uid;
private String uname;
private String upassword;
private int upower;
public int getUpower() {
return upower;
}
public void setUpower(int upower) {
this.upower = upower;
}
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getUpassword() {
return upassword;
}
public void setUpassword(String upassword) {
this.upassword = upassword;
}
}
(2)新建UserMapper.xml注意:它放在src/main/resources/mapper下代码如下
<mapper namespace="com.qbd.dao.UserDao">
<select id="find" parameterType="Map" resultMap="StudentResult">
select * from user
<where>
<if test="uname!=null and uname!='' ">
and uname like #{uname}
if>
where>
<if test="start!=null and size!=null">
limit #{start},#{size}
if>
select>
<select id="findbyid" parameterType="Integer" resultMap="StudentResult">
select * from user where uid=#{uid}
select>
<select id="getTotal" parameterType="Map" resultType="Long">
select count(*) from user
<where>
<if test="uname!=null and uname!='' ">
and uname like #{uname}
if>
where>
select>
<select id="getUser" resultMap="StudentResult" parameterType="com.qbd.model.User">
select *from user where uname=#{uname} and upassword=#{upassword}
select>
<delete id="delete" parameterType="Integer">
delete from user where uid=#{uid}
delete>
<update id="update" parameterType="com.qbd.model.User">
update user
<set>
<if test="uname!=null">
uname=#{uname},
if>
<if test="upassword!=null">
upassword=#{upassword},
if>
<if test="upower!=null">
upower=#{upower},
if>
set>
where uid=#{uid}
update>
<insert id="add" parameterType="com.qbd.model.User">
insert into user values(null,#{uname},#{upassword},#{upower})
insert>
<resultMap type="com.qbd.model.User" id="StudentResult">
<id property="uid" column="uid"/>
<result property="uname" column="uname"/>
<result property="upassword" column="upassword"/>
resultMap>
mapper>
(3)新建UserDao代码如下
package com.qbd.dao;
import java.util.List;
import java.util.Map;
import com.qbd.model.User;
public interface UserDao {
public List getAll();
public User getUser(User user);
public int delete(int id);
public int update(User user);
public int add(User user);
public List find(Map map);
public List findbyid(Integer uid);
public Long getTotal(Map map);
}
(4)然后分别新建service和serviceimpl,代码如下
package com.qbd.service;
import java.util.List;
import java.util.Map;
import com.qbd.model.User;
public interface UserService {
public List getAll();
public User getUser(User user);
public int delete(int id);
public int update(User user);
public int add(User user);
public List find(Map map);
public Long getTotal(Map map);
}
package com.qbd.serviceimpl;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qbd.dao.UserDao;
import com.qbd.model.User;
import com.qbd.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
@Autowired
private UserDao userDao;
public List getAll() {
// TODO Auto-generated method stub
return userDao.getAll();
}
public User getUser(User user) {
// TODO Auto-generated method stub
return userDao.getUser(user);
}
public int delete(int id) {
// TODO Auto-generated method stub
return userDao.delete(id);
}
public int update(User user) {
// TODO Auto-generated method stub
return userDao.update(user);
}
public int add(User user) {
// TODO Auto-generated method stub
return userDao.add(user);
}
public List find(Map map) {
// TODO Auto-generated method stub
return userDao.find(map);
}
public Long getTotal(Map map) {
// TODO Auto-generated method stub
return userDao.getTotal(map);
}
}
(5)新建controller
package com.qbd.controller;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.qbd.model.User;
import com.qbd.service.UserService;
import com.qbd.utils.PageBean;
import com.qbd.utils.StringUtil;
@Controller
public class UserController {
private UserService userService;
public UserService getUserService() {
return userService;
}
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
@RequestMapping("/userlist")
public Map<String, Object> getAll(String page, String rows, @ModelAttribute User user) throws Exception {
PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
Map<String, Object> map = new HashMap<String, Object>();
map.put("uname", StringUtil.formatLike(user.getUname()));
map.put("start", pageBean.getStart());
map.put("size", pageBean.getPageSize());
List<User> userList = userService.find(map);
Long total = userService.getTotal(map);
map.clear();
map.put("total", total);
map.put("users", userList);
return map;
}
@RequestMapping("/list")
public Map<String, Object> List() throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
map.put("uname", null);
map.put("start", null);
map.put("size", null);
List<User> userList = userService.find(map);
Long total = userService.getTotal(map);
map.clear();
map.put("total", total);
map.put("users", userList);
return map;
}
@RequestMapping("/usersave")
public Map<String, Object> save(@ModelAttribute User user) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
int resultTotal = 0; // �����ļ�¼����
if (user.getUid() == 0) {
resultTotal = userService.add(user);
} else {
resultTotal = userService.update(user);
}
if (resultTotal > 0) {
map.put("success", "保存成功");
} else {
map.put("success", "保存失败");
}
return map;
}
@RequestMapping("/userdelete")
public Map<String, Object> delete(@RequestParam(value = "deluids") String ids) throws Exception {
String[] idsStr = ids.split(",");
for (int i = 0; i < idsStr.length; i++) {
userService.delete(Integer.parseInt(idsStr[i]));
}
Map map = new HashMap();
map.put("success", "删除成功");
return map;
}
@RequestMapping("/index")
public String index() {
return "index";
}
}
(6)完成这些,证明你的基本编码已经完成,然而并没有配置,那么如何采用简单的springboot的配置那,首先在src/main/resources新建
application.properties内容如下
# 项目contextPath,一般在正式发布版本中,我们不配置
#server.context-path=/springbootmybatis
# 错误页,指定发生错误时,跳转的URL。请查看BasicErrorController源码便知
server.error.path=/error
# 服务端口
server.port=8888
# session最大超时时间(分钟),默认为30
server.session.timeout=60
# 该服务绑定IP地址,启动服务器时如本机不是该IP地址则抛出异常启动失败,只有特殊需求的情况下才配置
# server.address=192.168.16.11
# tomcat最大线程数,默认为200
server.tomcat.max-threads=800
# tomcat的URI编码
server.tomcat.uri-encoding=UTF-8
# 存放Tomcat的日志、Dump等文件的临时文件夹,默认为系统的tmp文件夹(如:C:\Users\Shanhy\AppData\Local\Temp)
server.tomcat.basedir=D:/log/springboot-tomcat-tmp
#数据库配置
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm
spring.datasource.username=root
spring.datasource.password=root
# mybatis_config
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.qbd.model
# 配置jsp文件的位置,默认位置为:src/main/webapp
#指向jsp文件位置:src/main/webapp/pages
spring.mvc.view.prefix: /pages/
spring.mvc.view.suffix: .jsp
(7)然后编写启动类来加载这些东西
package com.qbd;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
@SpringBootApplication
@MapperScan(basePackages = "com.qbd.dao")//这一步是让mapper文件实现dao接口的
public class SpringbootMybatisApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(SpringbootMybatisApplication.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(SpringbootMybatisApplication.class);
}
}
5、
然后运行,访问就可以得到结果。如果想要源代码可以点这个链接
http://download.csdn.net/detail/two_people/9821393