多平台统一管理软件接口,如何实现多平台统一管理软件接口
276
2023-01-25
详解springboot集成mybatis xml方式
springboot集成mybatis
关键代码如下:
1,添加pom引用
2 application.properties
mybatis.config-locations=classpath:mybatis/mybatis-config.xml
mybatis.mapper-locations=classpath:mybatis/mapper/*.xml
mybatis.type-aliases-package=com.kerry.model
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = 123456
3 在resource目录下创建mybatis目录并创建mybatis-config.xml文件
<typeAlias alias="ArrayList" type="java.util.ArrayList" />
mybatis目录下创建mapper目录存放mapper类接口文件
package com.kerry.mapper;
import java.util.List;
import com.kerry.model.User;
public interface UserMapper {
List
User getOne(Integer id);
void insert(User user);
void update(User user);
void delete(Integer id);
}
model类文件
package com.kerry.mapper;
import java.util.List;
import com.kerry.model.User;
public interface UserMapper {
List
User getOne(Integer id);
void insert(User user);
void update(User user);
void delete(Integer id);
}
userMapper.xml
id, name, age, address
SELECT
FROM user
SELECT
FROM user
WHERE id = #{id}
INSERT INTO
user
(id,name,age,address)
VALUES
(#{id},#{name}, #{age}, #{address})
UPDATE
user
SET
address = #{address}
WHERE
id = #{id}
DELETE FROM
user
WHERE
id =#{id}
controller:
package com.kerry.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.kerry.model.User;
import com.kerry.mapper.UserMapper;
@RestController
public class UserController {
@Autowired
private UserMapper userMapper;
@RequestMapping("/getUsers")
public List
List
return users;
}
@RequestMapping("/getUser")
public User getUser(Integer id) {
User user=userMapper.getOne(id);
return user;
}
@RequestMapping("/add")
public void save(User user) {
userMapper.insert(user);
}
@RequestMapping(value="update")
public void update(User user) {
userMapper.update(user);
}
@RequestMapping(value="/delete/{id}")
public void delete(@PathVariable("id") Integer id) {
userMapper.delete(id);
}
}
最后在启动类上加上 扫描maper interface注解
@SpringBootApplication
@MapperScan("com.kerry.mapper")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
或者在每个XXMapper类上加上@mapper注解也行 二选一即可
比如
package com.kerry.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import com.kerry.model.User;
@Mapper
public interface UserMapper {
List
User getOne(Integer id);
void insert(User user);
void update(User user);
void delete(Integer id);
}
个人建议使用直接在启动类上application上@MapperScan方便,一次搞定,不必每写一个mapper类都加上@mapper注解
附上项目结构目录 选中的类以及文件
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~