看到了一個項目裏什麼是字符串拼接的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
這兩個配置都可以