package com.dm.batis.mapper;import com.dm.batis.bean.Book; import org.apache.ibatis.annotations.*; import org.apache.ibatis.mapping.FetchType;import com.dm.batis.bean.User;import java.util.List;@Mapper public interface UserMapper {//一对一public User UserAddressById(int userId);//一对多public User UserBookById(int userId);}
package com.dm.batis.mapper;import java.util.List;import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select;import com.dm.batis.bean.Book; import org.springframework.data.repository.query.Param;@Mapper public interface BookMapper {@Select("select * from book where userId = #{userId}")public List getBooksByUserId(int userId);public List UserBookList(int userId);}
package com.dm.batis.mapper;import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Select;import com.dm.batis.bean.Address; @Mapper public interface AddressMapper {@Select("select * from address where userId=#{userId}")public Address getAddressByUserId(int userId);public Address getAddressById(int userId); }
5、在包目录下面建立目录controller
里面新建index
package com.dm.batis.controller;import com.dm.batis.bean.User; import com.dm.batis.mapper.BookMapper; import com.dm.batis.mapper.UserMapper; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;@RestController public class Index {@Autowiredprivate UserMapper userMapper;@Autowiredprivate BookMapper bookMapper;@RequestMapping("/index")public User index(){User user = userMapper.UserAddressById(1);//User user=userMapper.UserBookById(1);return user;} }