springboot整合springmvc和Mybatis,连接数据库的细节操作
1、建立项目
2、改pom文件
<!-- 修改jdk版本 --> <properties> <java.version>1.7</java.version> <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version> <thymeleaf-layout-dialect.version>2.0.4</thymeleaf-layout-dialect.version> </properties> <dependencies> <!-- springboot web启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- springboot thymeleaf启动器 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency> <!-- Mybatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </dependency> <!-- Mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!-- 数据库连接池 --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.0.9</version> </dependency> </dependencies>
在classpath/跟下起个applicattion.properties文件。内容如下
spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/bbs spring.datasource.username=root spring.datasource.password=root spring.datasource.type=com.alibaba.druid.pool.DruidDataSource mybatis.type-aliases-package = com.zhu.pojo //给包起别名
3、 创建实体类
package com.zhu.pojo; public class Users { private Integer id; private String name; private Integer age; public Users() { super(); } public Users(Integer id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
4、创建mapper接口和配置文件
编译时会出错,原因是xml文件少了头部
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
package com.zhu.mapper; import com.zhu.pojo.Users; public interface UsersMapper { void insertUser(Users users); }
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="com.zhu.mapper.UsersMapper">
<insert id = "insertUser" parameterType="users">
insert into user(name,age) values(#{name},#{age})
</insert>
</mapper>
5、业务层service和他的impl类
package com.zhu.service; import com.zhu.pojo.Users; public interface UsersService { void addUser(Users users); }
package com.zhu.impl; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import com.zhu.mapper.UsersMapper; import com.zhu.pojo.Users; import com.zhu.service.UsersService; @Service @Transactional public class SreviceImpl implements UsersService { @Autowired private UsersMapper usersMapper; @Override public void addUser(Users users) { this.usersMapper.insertUser(users); } }
6、创建comtroller 和index页面和成功页面
package com.zhu.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import com.zhu.pojo.Users; import com.zhu.service.UsersService; @Controller @RequestMapping("/user") public class UserController { private UsersService usersService; /* * 页面跳转 */ @RequestMapping("/{page}") public String showPage(@PathVariable String page) { return page; } /* * 添加用户 */ @RequestMapping("/addUser") public String addUser(Users users) { this.usersService.addUser(users); return "ok"; } }
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>添加用户</title> </head> <body> <form th:action="@{/user/addUser}" method = "post"> 用户名字:<input type="text" name="name"></input><br/> 用户年龄:<input type="text" name="age"></input><br/> <input type="submit" value ="确认"></input><br/> </form> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>成功</title> </head> <body> 操作成功 </body> </html>
7、启动类,注意启动类中药多一个注解@MapperScan("com.zhu.mapper") //用于扫描Mybatis的Mapper接口,不然会产生异常
Field usersMapper in com.zhu.impl.SreviceImpl required a bean of type 'com.zhu.mapper.UsersMapper' that could not be found.
其中MapperScan()里的路径是Mapper的路径
package com.zhu; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.zhu.mapper") //用于扫描Mybatis的Mapper接口 public class App { public static void main(String[] args) { SpringApplication.run(App.class, args); } }
文档根元素 "mapper" 必须匹配 DOCTYPE 根 "null"
启动项目的时候报了这个错,网上找了虽然没有完全匹配的,但是解决方法是一样的,mybatis的xml文件文件头问题,开发人员复制粘贴的时候少复制了部分内容,原来报错的写法:
<?xml version="1.0" encoding="UTF-8"?>
<mapper namespace="com.test.mapper.TestMapper">
<insert id="test" useGeneratedKeys="true">
xxx
</insert>
</mapper>
正确头文件应该加上
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">