搭建項(xiàng)目結(jié)構(gòu)
創(chuàng)建Maven模塊
搭建mysql數(shù)據(jù)庫結(jié)構(gòu)
1. 創(chuàng)建數(shù)據(jù)庫
2. 新建數(shù)據(jù)表并添加字段和約束
3. 完成后查看數(shù)據(jù)表
搭建項(xiàng)目結(jié)構(gòu)
1. 搭建包結(jié)構(gòu)
2. 在pom.xml中引入項(xiàng)目依賴(坐標(biāo))
會(huì)優(yōu)先去本地倉庫進(jìn)行查找,已有的會(huì)進(jìn)行提示在寫的時(shí)候,寫完記得刷新一下(右上角 沒有的會(huì)去中央倉庫進(jìn)行下載,聯(lián)網(wǎng)情況下
3. 在resource目錄下引入Mybatis.config配置文件和db.properties配置文件
driver=com.mysql.cj.jdbc.Driver // mysql驅(qū)動(dòng)
url=jdbc:mysql://localhost:3306/數(shù)據(jù)庫名
username=用戶名
password=密碼
// 8.0.30 數(shù)據(jù)庫連接是這樣
<properties resource="db.properties"></properties>
<!-- 連接Mysql數(shù)據(jù)庫-->
<environments default="development">
<environment id="development">
<transactionManager type="JDBC"></transactionManager>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<!-- SQL Mapper 接口所在包-->
<package name="com.itheima.mapper"/>
</mappers>
<settings>
<!-- Mybatis默認(rèn)日志-->
<setting name="logImpl" value="stdout_logging"/>
</settings>
搭建學(xué)員管理系統(tǒng)框架
1. 編寫MybatisUtil工具類
public class MybatisUtil {
// 保證在程序執(zhí)行期間只有一個(gè)Factory (工廠模式生成session對(duì)象)
private static SqlSessionFactory sqlSessionFactory;
// 靜態(tài)代碼塊來初始化 sqlSessionFactory
static {
try {
InputStream in = Resources.getResourceAsStream("MybatisConfig.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
// 提供一個(gè)公告方法供 其它類獲取session會(huì)話
public static SqlSession opSession() {
return sqlSessionFactory.openSession();
}
// 提交和關(guān)閉事務(wù)
public static void commitAndClose(SqlSession sqlSession) {
sqlSession.commit();
sqlSession.close();
}
}
2.編寫 Student 實(shí)體類
public class Student {
private Integer id;
private String name;
private String sex;
private Integer age ;
private Double height;
public Student() {
}
public Student(Integer id, String name, String sex, Integer age, Double height) {
this.id = id;
this.name = name;
this.sex = sex;
this.age = age;
this.height = height;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", sex='" + sex + '\'' +
", age=" + age +
", height=" + height +
'}';
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Double getHeight() {
return height;
}
public void setHeight(Double height) {
this.height = height;
}
}
3.編寫Mapper接口類
public interface StuMapper {
@Options(useGeneratedKeys = true,keyProperty = "id") // 開啟主鍵,Stu的id接收
@Insert("insert into student values(null,#{name},#{sex},#{age},#{height})")
public void save(Student stu);
@Select("select * from student")
public ArrayList<Student> findAll();
@Select("select * from student where id = #{id}")
public Student findStuById(Integer id);
@Update("update student set name = #{name}, sex = #{sex},age = #{age} , height = #{height} where id = #{id}")
public void updateStu(Student stu);
@Delete("delete from student where id =#{id}")
public void deleteStu(Integer id);
}
4.搭建主方法框架
public static void main(String[] args) {
while (true) {
System.out.println("********************************************************");
System.out.println("1.增加學(xué)員 2.刪除學(xué)員 3.修改學(xué)員 4.查詢學(xué)員 5.退出 ");
System.out.println("********************************************************");
System.out.println("請(qǐng)選擇您的選擇:(1-5)");
String op = sc.next();
switch (op) {
case "1":
addStu();
break;
case "2":
deleteStu();
break;
case "3":
updateStu();
break;
case "4":
findAll();
break;
case "5":
System.out.println("謝謝使用本系統(tǒng),拜拜!");
System.exit(0);
default:
System.out.println("您的輸入有誤!");
break;
}
}
}
5.實(shí)現(xiàn)學(xué)員信息管理(增刪改查)
①增加學(xué)員
private static void addStu() {
System.out.println("請(qǐng)輸入姓名:");
String name = sc.next();
System.out.println("請(qǐng)輸入性別:");
String sex = sc.next();
System.out.println("請(qǐng)輸入年齡:");
int age = sc.nextInt();
System.out.println("請(qǐng)輸入身高:");
double height = sc.nextDouble();
Student student = new Student();
student.setName(name);
student.setSex(sex);
student.setAge(age);
student.setHeight(height);
SqlSession sqlSession = MybatisUtil.opSession();
StuMapper mapper = sqlSession.getMapper(StuMapper.class);
mapper.save(student);
System.out.println("添加成功!主鍵id: " + student.getId());
MybatisUtil.commitAndClose(sqlSession);
}
② 刪除學(xué)員
private static void deleteStu() {
findAll();
System.out.println("請(qǐng)輸入要修改的學(xué)員學(xué)號(hào):");
int id = sc.nextInt();
SqlSession sqlSession = MybatisUtil.opSession();
StuMapper mapper = sqlSession.getMapper(StuMapper.class);
Student student = mapper.findStuById(id);
if (student == null) {
System.out.println("沒有該學(xué)員信息!");
return;
}
System.out.println("================================================================");
System.out.println("----------------------------------------------------------------");
System.out.println("學(xué)號(hào)\t\t姓名\t\t性別\t\t年齡\t\t身高");
System.out.println(student.getId()+"\t\t\t"+student.getName()+"\t\t\t"+student.getSex()+"\t\t"+
student.getAge()+"\t\t\t"+student.getHeight());
System.out.println("----------------------------------------------------------------");
System.out.println("請(qǐng)確認(rèn)是否刪除該學(xué)員:(Y/N)");
String op = sc.next();
if ("y".equalsIgnoreCase(op)) {
sqlSession = MybatisUtil.opSession();
mapper = sqlSession.getMapper(StuMapper.class);
mapper.deleteStu(id);
MybatisUtil.commitAndClose(sqlSession);
System.out.println("刪除成功!");
}
else {
System.out.println("取消修改!");
return;
}
}
③修改學(xué)員
private static void updateStu() {
findAll();
System.out.println("請(qǐng)輸入要修改的學(xué)員學(xué)號(hào):");
int id = sc.nextInt();
SqlSession sqlSession = MybatisUtil.opSession();
StuMapper mapper = sqlSession.getMapper(StuMapper.class);
Student student = mapper.findStuById(id);
if (student == null) {
System.out.println("沒有該學(xué)員信息!");
return;
}
//回顯數(shù)據(jù)
System.out.println("================================================================");
System.out.println("----------------------------------------------------------------");
System.out.println("學(xué)號(hào)\t\t姓名\t\t性別\t\t年齡\t\t身高");
System.out.println(student.getId()+"\t\t\t"+student.getName()+"\t\t\t"+student.getSex()+"\t\t"+
student.getAge()+"\t\t\t"+student.getHeight());
System.out.println("----------------------------------------------------------------");
System.out.println("請(qǐng)選擇是否修改姓名:(Y/N)");
String op = sc.next();
if ("y".equalsIgnoreCase(op)) {
System.out.println("請(qǐng)輸入新姓名:");
String newName = sc.next();
student.setName(newName);
}
System.out.println("請(qǐng)選擇是否修改性別:(Y/N)");
op = sc.next();
if ("y".equalsIgnoreCase(op)) {
System.out.println("請(qǐng)輸入新性別:");
String newNSex = sc.next();
student.setSex(newNSex);
}
System.out.println("請(qǐng)選擇是否修改年齡:(Y/N)");
op = sc.next();
if ("y".equalsIgnoreCase(op)) {
System.out.println("請(qǐng)輸入新年齡:");
int newAge = sc.nextInt();
student.setAge(newAge);
}
System.out.println("請(qǐng)選擇是否修改身高:(Y/N)");
op = sc.next();
if ("y".equalsIgnoreCase(op)) {
System.out.println("請(qǐng)輸入新身高:");
double newHheight = sc.nextDouble();
student.setHeight(newHheight);
}
sqlSession = MybatisUtil.opSession();
mapper = sqlSession.getMapper(StuMapper.class);
mapper.updateStu(student);
MybatisUtil.commitAndClose(sqlSession);
System.out.println("修改已完成!");
}
④ 查詢學(xué)員
private static void findAll() {
SqlSession sqlSession = MybatisUtil.opSession();
StuMapper mapper = sqlSession.getMapper(StuMapper.class);
ArrayList<Student> stus = mapper.findAll();
MybatisUtil.commitAndClose(sqlSession);
if (stus.size() == 0 || stus == null) {
System.out.println("暫無數(shù)據(jù)!");
return;
}
System.out.println("【學(xué)員信息】");
System.out.println("================================================================");
System.out.println("----------------------------------------------------------------");
System.out.println("學(xué)號(hào)\t\t姓名\t\t性別\t\t年齡\t\t身高");
for (Student student : stus) {
System.out.println(student.getId()+"\t\t\t"+student.getName()+"\t\t\t"+student.getSex()+"\t\t"+
student.getAge()+"\t\t\t"+student.getHeight());
}
System.out.println("----------------------------------------------------------------");
}
運(yùn)行結(jié)果
1. 添加功能測(cè)試
2.查詢功能測(cè)試
3.修改功能測(cè)試
4.刪除功能測(cè)試
文章來源:http://www.zghlxwxcb.cn/news/detail-562386.html
補(bǔ)充:關(guān)于Mapper接口下@SQL五提示為綠色問題
在sql語句中 alt + enter(回車)選擇如圖所示語句,搜索mysql點(diǎn)擊確認(rèn)就可以了文章來源地址http://www.zghlxwxcb.cn/news/detail-562386.html
到了這里,關(guān)于IDEA-Maven-Mybatis基礎(chǔ)框架搭建的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!