MayBaties 搭建学习汇总
独立搭建MyBaties时遇见的注意事项。搭建是用的project为Maven Project,使用工具为Spring Tool Suite 4
1.创建Maven project 项目后在pom.xml中添加下面代码获取mybaties需求的jar.。
<!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.5</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
2.在项目中创建controller,dao,entity 包,下图为学习创建的项目结构
3.在src目录下创建Mapper.xml 和Config.xml
需要注意的是该src目录是存放Java的根目录下面,存储位置错位会导致项目运行时无法找到配置文件。
Config.xml数据库配置信息
需要注意的是MySql数据库url配置信息
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ticket?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC"/><!--用自己的数据库名字 -->
<property name="username" value="root"/> <!-- 用自己的用户名密码 -->
<property name="password" value="qwer!123"/><!--用户密码-->
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="LotteryMapper.xml"/> <!-- 这里配置映射文件 -->
</mappers>
</configuration>
Mapper配置sql语句
<?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="lk.lottery.dao.TicketDao">
<select id="getTicket" resultType="lk.lottery.entity.TicketBeen" parameterType="String">
select * from user where ticket_id=#{id}
</select>
<select id="getAllMsg" resultType="lk.lottery.entity.TicketBeen"><!--been实体类-->
select * from ticket_msg
</select>
<insert id="addTicket" parameterType="lk.lottery.entity.TicketBeen">
insert into ticket_msg(ticket_id, ticket_code, ticket_time,codeOne,codeTwo,codeThree,codeFoure,codeFive,codeSix,codeBlue)
values(#{ticket_id},#{ticket_code},#{ticket_time},#{codeOne}, #{codeTwo},#{codeThree},#{codeFoure}, #{codeFive}, #{codeSix},#{codeBlue})
</insert>
</mapper>
4.创建been、dao.controller层
been层
package lk.lottery.entity;
public class TicketBeen {
private static String ticket_time;
private static String ticket_code;
private static int codeOne;
private static int codeTwo;
private static int codeThree;
private static int codeFoure;
private static int codeFive;
private static int codeSix;
private static int codeBlue;
private static String ticket_id;
public static String getTicket_time() {
return ticket_time;
}
public static void setTicket_time(String ticket_time) {
TicketBeen.ticket_time = ticket_time;
}
public static String getTicket_code() {
return ticket_code;
}
public static void setTicket_code(String ticket_code) {
TicketBeen.ticket_code = ticket_code;
}
public static int getCodeOne() {
return codeOne;
}
public static void setCodeOne(int codeOne) {
TicketBeen.codeOne = codeOne;
}
public static int getCodeTwo() {
return codeTwo;
}
public static void setCodeTwo(int codeTwo) {
TicketBeen.codeTwo = codeTwo;
}
public static int getCodeThree() {
return codeThree;
}
public static void setCodeThree(int codeThree) {
TicketBeen.codeThree = codeThree;
}
public static int getCodeFoure() {
return codeFoure;
}
public static void setCodeFoure(int codeFoure) {
TicketBeen.codeFoure = codeFoure;
}
public static int getCodeFive() {
return codeFive;
}
public static void setCodeFive(int codeFive) {
TicketBeen.codeFive = codeFive;
}
public static int getCodeSix() {
return codeSix;
}
public static void setCodeSix(int codeSix) {
TicketBeen.codeSix = codeSix;
}
public static int getCodeBlue() {
return codeBlue;
}
public static void setCodeBlue(int codeBlue) {
TicketBeen.codeBlue = codeBlue;
}
public static String getTicket_id() {
return ticket_id;
}
public static void setTicket_id(String ticket_id) {
TicketBeen.ticket_id = ticket_id;
}
public TicketBeen(String ticket_time, String ticket_code, int codeOne, int codeTwo, int codeThree, int codeFoure,
int codeFive, int codeSix, int codeBlue, String ticket_id) {
super();
TicketBeen.ticket_time = ticket_time;
TicketBeen.ticket_code = ticket_code;
TicketBeen.codeOne = codeOne;
TicketBeen.codeTwo = codeTwo;
TicketBeen.codeThree = codeThree;
TicketBeen.codeFoure = codeFoure;
TicketBeen.codeFive = codeFive;
TicketBeen.codeSix = codeSix;
TicketBeen.codeBlue = codeBlue;
TicketBeen.ticket_id = ticket_id;
}
}
dao层
创建dao层是需要注意add方法的返回类型 ,add方法无法返回String类型 可以返回boolean,int
package lk.lottery.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import lk.lottery.entity.TicketBeen;
public interface TicketDao {
/**新增数据**/
public int addTicket(TicketBeen ticketMsg);
/**单个数据查询**/
public TicketBeen getTicket(String id);
/**查询导入票码全部**/
public List getAllMsg();
}
controller层
package lk.lottery.controller;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.UUID;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import lk.lottery.dao.TicketDao;
import lk.lottery.entity.TicketBeen;
public class AddTicket {
private static Scanner input;
public static void main(String[] args) throws IOException {
scanerIn();
}
public static String scanerIn() throws IOException {
String resource="SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(inputStream);
SqlSession session = factory.openSession();
String return_Msg=null;
input = new Scanner(System.in);
System.out.println("请输入 上一期红球码(以,分割输入):");
String ticket_num =input.next();
System.out.println("输入蓝球");
int blueb=input.nextInt();
System.out.println("输入日期");
String date=input.next();
String [] num=ticket_num.split(",");
// 对数组进行排序
Arrays.sort(num);
Map<Integer, Integer> map=new HashMap<Integer, Integer>();
for(int i=0;i<num.length;i++) {
String getnum=num[i];
map.put(Integer.valueOf(i), Integer.valueOf(getnum));
}
String id=UUID.randomUUID().toString();
TicketBeen ticketMsg = new TicketBeen(date,ticket_num, map.get(0).intValue(),
map.get(1).intValue(), map.get(2).intValue(), map.get(3).intValue(),
map.get(4).intValue(), map.get(5).intValue(), blueb, id);
TicketDao mapper =session.getMapper(TicketDao.class);
//boolean boold=mapper.addTicket(ticketMsg);
int boold=mapper.addTicket(ticketMsg);
if (boold==1) {
System.err.println("存储数据库成功!");
return_Msg="success";
}else {
System.err.println("存储数据库失败!");
return_Msg="fault";
}
//System.err.println("返回存入的结果++++++"+boold);
session.commit();
return return_Msg;
}
}
5.实现结果