pom文件里添加依赖
<!-- 数据库需要的依赖 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
application.properties
我用的是springBoot 2.1 不需要配置驱动,配置驱动的话反而会出现警告
#数据库
spring.datasource.url=jdbc:mysql://192.168.238.130:3306/dev?useUniode=true&characterEncoding=utf-8
spring.datasource.username=root
spring.datasource.password=123
Mapper.java文件
import java.util.ArrayList; import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Many;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select; import com.sc.Firstboot.entity.User; public interface MainMapper { /**
* 简单查询的话可以用下面这中注解的方式,比较简洁
* 复杂查询还是需要用到mapper.xml配置文件来完成
* @return
*/ @Select("select * from user")
public ArrayList<User> find(); @Insert("insert into user(id,username,age,pwd) values(#{id},#{username},#{age},#{pwd})")
public int insert(@Param("id") String id, @Param("username") String username, @Param("age") int age,
@Param("pwd") String pwd); public ArrayList<User> findAllAndPro();
}
Mapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.sc.Firstboot.dao.MainMapper"> <select id="findAllAndPro" resultMap="userMap">
select
us.id,us.username,us.pwd,us.age,pru.id as pid ,
pru.productName,pru.price,pru.total
from user us
left join product pru on pru.uid = us.id
</select> <!-- 注意:当多个表的字段名一样的时候,查询需要用别名,否则查询结果不理想 -->
<resultMap id="userMap" type="com.sc.Firstboot.entity.User">
<id property="id" column="id" />
<result property="userName" column="userName" />
<result property="pwd" column="pwd" />
<result property="age" column="age" />
<collection property="pList" ofType="com.sc.Firstboot.entity.Product">
<id property="id" column="pid" />
<result property="productName" column="productName" />
<result property="total" column="total" />
</collection>
</resultMap> </mapper>
service
@Service
public class MainService { @Autowired
private MainMapper mainMapper; public String insert(User user){
if(1 == mainMapper.insert("ewq", user.getUserName(),user.getAge(), user.getPwd()))return "success";
return "faild";
} public ArrayList<User> findAll(){
return mainMapper.find();
} public ArrayList<User> findAllAndPro(){
return mainMapper.findAllAndPro();
} }
controller
@Controller
public class MainController { @Autowired
private MainService mainSerice; /**
* 传参的时候需要注意区分命名,尽量不要使用相同的命名,
* 否则的框架会给形参列表中所有的实体里所有相同的属性名都赋值
* 例如 User实体和Product实体中都有id属性
* 那么传参的时候如果 id=XXX 那么两个实体的id都会被赋值XXX
* @param user
* @param product
* @return
*/
@ResponseBody
@RequestMapping(path = {"/insertUser"}, method = {RequestMethod.GET, RequestMethod.POST})
public String insertUser(User user,Product product){
System.out.println(user.toString());
System.out.println(product.toString());
return mainSerice.insert(user);
} @ResponseBody
@RequestMapping(path = {"/findAll"}, method = {RequestMethod.GET, RequestMethod.POST})
public ModelAndView findAll(){
ModelAndView mv = new ModelAndView();
Map<String,Object> map = new HashMap<>();
map.put("userList",mainSerice.findAll());
mv.setViewName("login/login");
mv.addObject("result",map);
return mv;
} @ResponseBody
@RequestMapping(path = {"/findAllAndPro"}, method = {RequestMethod.GET, RequestMethod.POST})
public ModelAndView findAllAndPro(){
ModelAndView mv = new ModelAndView();
Map<String,Object> map = new HashMap<>();
map.put("userList",mainSerice.findAllAndPro());
mv.setViewName("login/login");
mv.addObject("result",map);
return mv;
}
}
Application.java
/**
* 启动mapper文件的扫描注解
* 他会去制定的包范围下扫描所有以Mapper接吻的java文件
* 默认扫整个项目
* @author lenovo
*
*/
@SpringBootApplication
@MapperScan(basePackages={"com.sc.myProject.dao"})
public class Application { public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}