搭建项目结构
创建Maven模块
搭建mysql数据库结构
1. 创建数据库
2. 新建数据表并添加字段和约束
3. 完成后查看数据表
搭建项目结构
1. 搭建包结构
2. 在pom.xml中引入项目依赖(坐标)
会优先去本地仓库进行查找,已有的会进行提示在写的时候,写完记得刷新一下(右上角 没有的会去中央仓库进行下载,联网情况下
3. 在resource目录下引入Mybatis.config配置文件和db.properties配置文件
driver=com.mysql.cj.jdbc.Driver // mysql驱动
url=jdbc:mysql://localhost:3306/数据库名
username=用户名
password=密码
// 8.0.30 数据库连接是这样
<properties resource="db.properties"></properties>
<!-- 连接Mysql数据库-->
<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默认日志-->
<setting name="logImpl" value="stdout_logging"/>
</settings>
搭建学员管理系统框架
1. 编写MybatisUtil工具类
public class MybatisUtil {
// 保证在程序执行期间只有一个Factory (工厂模式生成session对象)
private static SqlSessionFactory sqlSessionFactory;
// 静态代码块来初始化 sqlSessionFactory
static {
try {
InputStream in = Resources.getResourceAsStream("MybatisConfig.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(in);
} catch (IOException e) {
e.printStackTrace();
}
}
// 提供一个公告方法供 其它类获取session会话
public static SqlSession opSession() {
return sqlSessionFactory.openSession();
}
// 提交和关闭事务
public static void commitAndClose(SqlSession sqlSession) {
sqlSession.commit();
sqlSession.close();
}
}
2.编写 Student 实体类
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.增加学员 2.删除学员 3.修改学员 4.查询学员 5.退出 ");
System.out.println("********************************************************");
System.out.println("请选择您的选择:(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("谢谢使用本系统,拜拜!");
System.exit(0);
default:
System.out.println("您的输入有误!");
break;
}
}
}
5.实现学员信息管理(增删改查)
①增加学员
private static void addStu() {
System.out.println("请输入姓名:");
String name = sc.next();
System.out.println("请输入性别:");
String sex = sc.next();
System.out.println("请输入年龄:");
int age = sc.nextInt();
System.out.println("请输入身高:");
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);
}
② 删除学员
private static void deleteStu() {
findAll();
System.out.println("请输入要修改的学员学号:");
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("没有该学员信息!");
return;
}
System.out.println("================================================================");
System.out.println("----------------------------------------------------------------");
System.out.println("学号\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("请确认是否删除该学员:(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;
}
}
③修改学员
private static void updateStu() {
findAll();
System.out.println("请输入要修改的学员学号:");
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("没有该学员信息!");
return;
}
//回显数据
System.out.println("================================================================");
System.out.println("----------------------------------------------------------------");
System.out.println("学号\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("请选择是否修改姓名:(Y/N)");
String op = sc.next();
if ("y".equalsIgnoreCase(op)) {
System.out.println("请输入新姓名:");
String newName = sc.next();
student.setName(newName);
}
System.out.println("请选择是否修改性别:(Y/N)");
op = sc.next();
if ("y".equalsIgnoreCase(op)) {
System.out.println("请输入新性别:");
String newNSex = sc.next();
student.setSex(newNSex);
}
System.out.println("请选择是否修改年龄:(Y/N)");
op = sc.next();
if ("y".equalsIgnoreCase(op)) {
System.out.println("请输入新年龄:");
int newAge = sc.nextInt();
student.setAge(newAge);
}
System.out.println("请选择是否修改身高:(Y/N)");
op = sc.next();
if ("y".equalsIgnoreCase(op)) {
System.out.println("请输入新身高:");
double newHheight = sc.nextDouble();
student.setHeight(newHheight);
}
sqlSession = MybatisUtil.opSession();
mapper = sqlSession.getMapper(StuMapper.class);
mapper.updateStu(student);
MybatisUtil.commitAndClose(sqlSession);
System.out.println("修改已完成!");
}
④ 查询学员
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("暂无数据!");
return;
}
System.out.println("【学员信息】");
System.out.println("================================================================");
System.out.println("----------------------------------------------------------------");
System.out.println("学号\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("----------------------------------------------------------------");
}
运行结果
1. 添加功能测试
2.查询功能测试
3.修改功能测试
4.删除功能测试
文章来源:https://www.toymoban.com/news/detail-562386.html
补充:关于Mapper接口下@SQL五提示为绿色问题
在sql语句中 alt + enter(回车)选择如图所示语句,搜索mysql点击确认就可以了
文章来源地址https://www.toymoban.com/news/detail-562386.html
到了这里,关于IDEA-Maven-Mybatis基础框架搭建的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!