看到了一个项目里什么是字符串拼接的sql写了一个插入语句,感觉快要崩溃了,然后还是使用了mybatis。做一个小的总结 :
引入依赖
在pom文件引入mybatis-spring-boot-starter的依赖:
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</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.0.29</version>
</dependency>
引入数据源
application.properties配置文件中引入数据源:
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
这样,springboot就可以访问数据了
具体实现
这篇文篇通过注解的形式实现。
创建实体
public class OrderGuest implements Serializable {
private Long id;
/**
* 订单号
*/
private String orderId;
/**
* 入住人-名
*/
private String fristName;
/**
* 入住人-姓
*/
private String lastName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getOrderId() {
return orderId;
}
public void setOrderId(String orderId) {
this.orderId = orderId;
}
public String getFristName() {
return fristName;
}
public void setFristName(String fristName) {
this.fristName = fristName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
Dao层
@Mapper
public interface OrderDao {
/**
* 添加order_guest
*
* @param name
* @param money
* @return
*/
@Insert("INSERT INTO order_guest (orderId, fristName, lastName) VALUES (#{orderId}, #{fristName}, #{lastName})")
int insertOrderGuest(@Param("orderId") String orderId, @Param("fristName") String fristName,
@Param("lastName") String lastName);
/**
*
* 添加 order_price
*
* @return
*/
@Insert("INSERT INTO order_price (orderId, priceDay, roomPrice) VALUES (#{orderId}, #{priceDay}, #{roomPrice})")
int insertOrderPrice(@Param("orderId") String orderid, @Param("priceDay") Date priceDay,
@Param("roomPrice") BigDecimal roomPrice);
/**
* 添加order_rateplan
* @param orderRatePlan
* @return
*/
@Insert("INSERT INTO order_rateplan ( `orderId`, `ratePlanName`, `ratePlanId`, `maxOccupancy`, `breakfastType`, `bedType`, "
+ "`confirmType`, `cannelType`, `cannelPolicy`, `roomTypeId`, `roomTypeName`)"
+ " VALUES (#{orderId},#{ratePlanName}, #{ratePlanId}, #{maxOccupancy}, #{breakfastType}, #{bedType}, #{confirmType},#{cannelType}, "
+ "#{cannelPolicy}, #{roomTypeId},#{roomTypeName})"
+ "")
int insertOrderRateplan(OrderRatePlan orderRatePlan);
@Update("UPDATE order_main SET orderState=#{orderState} where orderId=#{}")
int updateOrderMainByOrderId(@Param("orderState")Integer orderState, @Param("orderId")String orderId);
@Delete("delete from account where id = #{id}")
int delete(int id);
@Select("select id, name as name, money as money from account")
List<Account> findAccountList();
@Select("select id, name as name, money as money from account where id = #{id}")
Account findAccount(@Param("id") int id);
}
可以用@param注解接收参数,也可以用一个对象自动对应如:
@Insert("INSERT INTO order_rateplan ( `orderId`, `ratePlanName`, `ratePlanId`, `maxOccupancy`, `breakfastType`, `bedType`, "
+ "`confirmType`, `cannelType`, `cannelPolicy`, `roomTypeId`, `roomTypeName`)"
+ " VALUES (#{orderId},#{ratePlanName}, #{ratePlanId}, #{maxOccupancy}, #{breakfastType}, #{bedType}, #{confirmType},#{cannelType}, "
+ "#{cannelPolicy}, #{roomTypeId},#{roomTypeName})"
+ "")
int insertOrderRateplan(OrderRatePlan orderRatePlan);
service层
@Service
public class CreateReservationServiceImpl {
@Autowired
private OrderDao orderDao;
public Integer inserOrderGuest(List<OrderGuest> orderGuestList) {
Integer count = 0;
for (int i = 0; i < orderGuestList.size(); i++) {
OrderGuest orderGuest = orderGuestList.get(i);
String orderId = orderGuest.getOrderId();
String fristName = orderGuest.getFristName();
String lastName = orderGuest.getLastName();
Integer result = orderDao.insertOrderGuest(orderId, fristName, lastName);
System.out.println("执行结果:" + result);
if (result == 1) {
count = count + 1;
}
}
return count;
}
}
controllern层
@Controller
public class CreateReservationController {
@Autowired
private CreateReservationServiceImpl createReservationServiceImpl;
@Autowired
private HotelPriceRuleCommon hotelPriceRuleCommon;
public static String SERVRURL = "http://localhost:8081/ihotelmq/";
@RequestMapping(value = "/testSql", method = RequestMethod.POST)
@ResponseBody
public String test() {
List<OrderRatePlan> orderGuestList = new ArrayList<OrderRatePlan>();
OrderRatePlan orderRatePlan = new OrderRatePlan();
orderRatePlan.setOrderId("1903201614231191test");
orderRatePlan.setRatePlanName("高级房(Superior Room)");
orderRatePlan.setRatePlanId("100100100113");
orderRatePlan.setMaxOccupancy(2);
orderRatePlan.setBreakfastType("含早");
orderRatePlan.setBedType("大床");
orderRatePlan.setConfirmType(0);
orderRatePlan.setCannelType(0);
orderRatePlan.setCannelPolicy("[{\"Amount\": 1774,\"FromDate\": \"2019-08-09 00:00:00\"}]");
orderRatePlan.setRoomTypeId("1008");
orderGuestList.add(orderRatePlan);
createReservationServiceImpl.insertOrderRateplan(orderGuestList);
return "success";
}
}
为了方便需要配置日志打印sql语句
application.properties中添加:
mybatis.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
或者
logging.level.com.e100.hotelcore.Dao=DEBUG
这两个配置都可以