一、点击idea右上角的File,点击展开的菜单栏的new一栏,点击弹框的Maven选项,如下图所示,或者选择
,创建后的目录为
本章主要是用下图方式创建,选中红框对应的项目,点击next;
二、填写GroupId和ArtifactId,点击next:
三、选择本地安装的maven,maven的本地安装参考https://www.cnblogs.com/hglibin/p/9693901.html,在maven的安装过程中,可以配置maven目录(自己的:D:\tool\java\apache-maven-3.6.0\conf)下的settings.xml的maven库为下图(阿里的库)这个,这样创建maven项目后,项目初始化的过程更快。
为了快一点创建,我们添加一个属性值,如下图中的步骤第④步所示,点右边的加号,name=archetypeCatalog value=internal
点击下方的Enable Auto-Import(为了在pom.xml文件中添加依赖之后自动引入jar包)
控制台出现maven excution finish,说明项目创建成功
项目的目录如下
四:点击左上角的File-》Project Structure,出现如下界面,项目的modules已经添加了web
并且Artificts中已经配置好了项目打包的形式,如下图所示,以war包形式打包,也可以点击左上角的绿色“+”,增加其他类型的项目打包形式如:jar
五:点击modules,选择右边的source选项,配置我们maven项目中一般情况下的目录,目的也是为了便于项目管理,如下图所示,右键main目录,选中new Folder,①创建文件夹,②选中文件,设置文件类型
创建之后的目录如下:
六:在pom.xml文件中导入需要的jar包,在中,这个用的mysql数据库,如下:
org.mybatis
mybatis
3.3.0
mysql
mysql-connector-java
5.1.29
junit
junit
4.11
test
org.slf4j
slf4j-api
1.7.25
org.slf4j
slf4j-log4j12
1.7.7
log4j
log4j
1.2.17
七:创建mybatis的核心配置文件mybatis-config.xml
1.在main的resources资源文件下创建mybatis-config.xml,首先在IDEA中配置mybatis-config.xml模板,①点击加号,②
配置文件模板如下:
创建mybatis配置文件,如下:
在main->resources下新建数据库配置文件db.properties,内容如下:
characterEncoding=utf8的配置是为了与数据库的表结构的格式一样,避免表中的中文编程“?”的乱码。
database.driver=com.mysql.jdbc.Driver
database.url=jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8
database.username=root
database.password=root
在main->resources下新建日志配置文件log4j.properties,内容如下:
#全局的日志输出
log4j.rootLogger = TRACE,stdout
#mybatis日志配置
mapper.StudentMapper=TRACE
#控制台输出配置
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
2.编写数据库映射文件StudentMapper.xml
跟上面的mybatis-config.xml一样,先创建常用模板(新增,查询,删,修)模板内容如下:
StudentMapper.xml的内容如下:
INSERT INTO student(sno,name,sex,age,dept_no)
VALUES (#{sno},#{name},#{sex},#{age},#{dept_no})
UPDATE student SET name
=#{name},sex=#{sex},age=#{age},dept_no=#{dept_no}
where sno=#{sno}
DELETE FROM student
WHERE sno=#{sno}
3.创建与StudentMapper.xml对应的接口文件StudentMapper
内容如下:
public interface StudentMapper {
public Student getStudent(String sno);
public int addStudent(Student student);
public List getStudentAll();
public int updateStudent(Student student);
public int deleteStudent(String sno);
}
4.mybatis-config.xml配置mapper的信息
5.pom.xml的中配置如下相关信息
${basedir}/src/main/java
**/*.properties
**/*.xml
${basedir}/src/main/resources
6.实体类代码
package com.mialab.entity;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
public class Student {
private String sno;
private String name;
private String sex;
private int age;
private String dept_no;
@Override
public String toString()
{
return sno+","+name+","+sex+","+age+","+dept_no;
}
}
7.最后可以创建测试类进行测试构建的方法
package com.mialab.maintest;
import com.mialab.entity.Student;
import com.mialab.mapper.StudentMapper;
import com.mialab.utils.DBOperatorManager;
import org.apache.ibatis.session.SqlSession;
import java.util.List;
import java.util.logging.Logger;
/**
* Created by ZQ on 2019/12/28.
*/
public class test1 {
public static void main(String[] args)
{
//testSelectAll();
//testInsert();
//testUpdate();
testSelect("20191201");
}
private static void testDelete(String sno) {
Logger log = Logger.getLogger(test1.class.getName());
SqlSession session = null;
try {
session = DBOperatorManager.getInstance().getSqlSessionFactory().openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
mapper.deleteStudent(sno);
session.commit();
} catch (Exception ex) {
session.rollback();
ex.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
private static void testSelectAll()
{
Logger log=Logger.getLogger(test1.class.getName());
SqlSession session=null;
try {
session= DBOperatorManager.getInstance().getSqlSessionFactory().openSession();
StudentMapper mapper=session.getMapper(StudentMapper.class);
List studentList=mapper.getStudentAll();
for (Student stu :studentList)
{
log.info(stu.toString());
//System.out.println(stu);
}
}
finally {
if (session!=null)
{
session.close();
}
}
}
private static void testSelect(String sno)
{
Logger log=Logger.getLogger(test1.class.getName());
SqlSession session=null;
try {
session= DBOperatorManager.getInstance().getSqlSessionFactory().openSession();
StudentMapper mapper=session.getMapper(StudentMapper.class);
Student student=mapper.getStudent(sno);
log.info(student.toString());
//System.out.println(student);
}
finally {
if (session!=null)
{
session.close();
}
}
}
private static void testInsert() {
Logger log = Logger.getLogger(test1.class.getName());
SqlSession session = null;
try {
session = DBOperatorManager.getInstance().getSqlSessionFactory().openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student=new Student();
student.setSno("20191228");
student.setName("李白");
student.setAge(68);
student.setSex("男");
student.setDept_no("2610");
log.info(student.toString());
//System.out.println(student);
mapper.addStudent(student);
session.commit();
} catch (Exception ex) {
session.rollback();
ex.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
private static void testUpdate() {
Logger log = Logger.getLogger(test1.class.getName());
SqlSession session = null;
try {
session = DBOperatorManager.getInstance().getSqlSessionFactory().openSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);
Student student=new Student();
student.setSno("20191201");
student.setAge(68);
student.setName("小强");
student.setSex("女");
student.setDept_no("2611");
log.info(student.toString());
//System.out.println(student);
mapper.updateStudent(student);
session.commit();
} catch (Exception ex) {
session.rollback();
ex.printStackTrace();
} finally {
if (session != null) {
session.close();
}
}
}
}