ITKeyword,专注技术干货聚合推荐

注册 | 登录

Maven + Spring Boot + Mybatis 框架整合

xieke90 分享于

2020腾讯云双十一活动,全年最低!!!(领取3500元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1073

2020阿里云最低价产品入口,含代金券(新老用户有优惠),
地址https://www.aliyun.com/minisite/goods

推荐:【MyBatis框架】mybatis和spring整合

spring和mybatis整合 1.整合思路 需要spring通过单例方式管理SqlSessionFactory。 spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession。(s

整合过程介绍(开发工具:IntelliJ IDEA ) 项目结构如下:

1、新建springboot maven项目,pom.xml配置如下: <?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.xieke.test</groupId>

<artifactId>springbootmybatisdemo</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>jar</packaging>

<name>springbootmybatisdemo</name>

<description>Demo project for Spring Boot Mybatis</description>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.0.2.RELEASE</version>

<relativePath/> <!-- lookup parent from repository -->

</parent>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

<java.version>1.8</java.version>

</properties>

<dependencies>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.3.2</version>

</dependency>

<dependency>

<groupId>mysql</groupId>

<artifactId>mysql-connector-java</artifactId>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-test</artifactId>

<scope>test</scope>

</dependency>

</dependencies>

<build>

<plugins>

<plugin>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-maven-plugin</artifactId>

</plugin>

</plugins>

</build></project>

2、添加application.properties文件到 resources下,配置如下: server:

port: 1314#配置数据源spring:

datasource:

url: jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf8

username: root

password: 999999

driver-class-name: com.mysql.jdbc.Driver#指定mybatis映射文件的地址mybatis:

mapper-locations: classpath:mapper/*.xml# 打印sqllogging:

level:

com.xieke.test.dao : debug

3、SQL初始化文件table.sql,内容如下: use test;drop table `user`;create table `user` (

`id` int(10) NOT NULL AUTO_INCREMENT,

`name` varchar(25) NOT NULL,

`password` varchar(25) NOT NULL,

PRIMARY KEY (`id`) );insert into `user` (`id`, `name`, `password`) values('1','xieke','999999');insert into `user` (`id`, `name`, `password`) values('2','43434','324233');insert into `user` (`id`, `name`, `password`) values('3','34344','343434'); 4、控制层、业务层、持久层、实体类代码如下: package com.xieke.test.controller;import com.xieke.test.entity.User;import com.xieke.test.service.UserService;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;@Controller@RequestMapping(value = "/user")public class UserController {

@Autowired

pr

推荐:spring springmvc mybatis maven整合

原创内容,转载请注明出处 1.概述 maven项目目录如下

src/main/java:主要java文件路径 src/main/resources:主要资源文件路径 src/test/java:java测试文件目

ivate UserService userService;

@RequestMapping(value = "/show")

@ResponseBody

public String show(@RequestParam(value = "name")String name){

User user = userService.findUserByName(name);

if(null != user)

return user.getId()+"/"+user.getName()+"/"+user.getPassword();

else

return "null";

}

@RequestMapping(value = "/show2")

@ResponseBody

public String show2(@RequestParam(value = "name")String name, @RequestParam(value = "password")String password){

User user = userService.findUserByNameAndPassword(name, password);

if(null != user)

return user.getId()+"/"+user.getName()+"/"+user.getPassword();

else

return "null";

}

@RequestMapping(value = "/add")

@ResponseBody

public String add(){

userService.saveUser();

User user = userService.findUserByName("a123456789");

if(null != user)

return user.getId()+"/"+user.getName()+"/"+user.getPassword();

else

return "null";

}}

package com.xieke.test.service;import com.xieke.test.dao.UserDao;import com.xieke.test.entity.User;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Servicepublic class UserService {

@Autowired

private UserDao userDao;

public User findUserByName(String name){

User user = null;

try{

user = userDao.findUserByName(name);

} catch (Exception e){

}

return user;

}

public User findUserByNameAndPassword(String name, String

password){

User user = null;

try{

User u = new User();

u.setName(name);

u.setPassword(password);

user = userDao.findUserByNameAndPassword(u);

} catch (Exception e){

}

return user;

}

@Transactional

public void saveUser ( ){

userDao.insertUser("a123456789", "123456789");

// int n = 1/0;

// userDao.insertUser("b123456789", "123456789");

}}

package com.xieke.test.dao;import com.xieke.test.entity.User;import org.apache.ibatis.annotations.Insert;import org.apache.ibatis.annotations.Mapper;import org.apache.ibatis.annotations.Param;import org.apache.ibatis.annotations.Select;@Mapperpublic interface UserDao {

@Select("SELECT * FROM user WHERE name = #{name}")

User findUserByName(@Param("name") String name);

@Insert("INSERT INTO user(name, password) VALUES(#{name}, #{password})")

int insertUser(@Param("name") String name, @Param("password") String password);

User findUserByNameAndPassword (User

user);}

package com.xieke.test.entity;public class User {

private long id;

private String name;

private String password;

public long getId() {

return id;

}

public void setId(long id) {

this.id = id;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}}

5、启动类代码如下,启动后访问localhost:1314/user/show?name=xieke测试,详细代码请参考码云。

package com.xieke.test;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.transaction.annotation.EnableTransactionManagement;@SpringBootApplication@EnableTransactionManagementpublic class SpringbootmybatisdemoApplication {

public static void main(String[] args) {

SpringApplication.run(SpringbootmybatisdemoApplication.class, args);

}}

推荐:Maven+Spring+SpringMVC+MyBatis框架搭建

my git : https://github.com/hejiawang          看了一段时间Android,学了学C++,搭个SSM的框架复习复习老本行。        原来的SSH——Struts,spring,hibe

整合过程介绍(开发工具:IntelliJ IDEA ) 项目结构如下:   1、新建springboot maven项目,pom.xml配置如下: <xml version="1.0" encoding="UTF-8"><project xmlns="http://maven.apache

相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。