mybatis 数据库 6.8k

1 依赖

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<!--notice 数据库连接-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.0.1</version>
		</dependency>

		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<version>1.16.10</version>
		</dependency>

2 application。xml 配置

spring:
  # 数据源配置  //spring.datasource.url
  datasource:
    url: jdbc:mysql://localhost:3306/drog?useUnicode=true&characterEncoding=utf8&useSSL=false&tinyInt1isBit=true
    username: root
    password: 123456

3 application 扫描配置

@SpringBootApplication
@MapperScan("com.mybatisplus.mapper")//扫描mapper包下的类到spring容器中
public class MybatisplusApplication {

	public static void main(String[] args) {
		SpringApplication.run(MybatisplusApplication.class, args);
	}
}

 

4 dao对象生成

public interface UserMapper extends BaseMapper<User> {
}

5 代码中使用

/@RequestMapping("/demo")
// notice:   
// notice: @RequestMapping:提供路由信息,负责URL到Controller中的具体函数的映射
@Controller
public class UserController {
    @Autowired
    UserMapper userMapper;

    //   notice:   通过 http://localhost:8080/user访问该方法
    // notice:   @ResponseBody:表示该方法的返回结果直接写入HTTP response body中
    // notice:  在使用@RequestMapping后,返回值通常解析为跳转路径
    // notice:  加上@esponsebody后返回结果不会被解析为跳转路径,而是直接写入HTTP response body中。
    // notice:  比如异步获取json数据,加上@Responsebody后,会直接返回json数据。该注解一般会配合@RequestMapping一起使用
    @RequestMapping("/user")
    @ResponseBody
    public String insert(){
        User user = new User();
        user.setAge(12);
        user.setName("zxl");
        user.setEmail("zxl@163.com");
       userMapper.insert(user);
       return "ok";
    }

    // notice:  不加@esponsebody返回结果会被解析为跳转路径
    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @PostMapping("/login")
    @ResponseBody//返回json                                           //@RequestBody接受json格式数据
    public Object login(@RequestParam("userName") String userName, @RequestParam("password") String password ){//, @RequestBody String obj
        User user = new User();
        user.setAge(12);
        user.setName(userName);
        user.setEmail(password);
        return user;
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值