微软推荐的分层式结构一般分为三层,从下至上分别为:数据访问层、业务逻辑层、表示层。分层式设计可以达至如下目的:分散关注、松散耦合、逻辑复用、标准定义。
JavaEE中分层架构:表现层、业务层、持久层
轻量级开发中采用的是:表现层、业务层、持久层、域对象层
表现层:MVC模式,SpringMVC,收集客户动作和显示业务数据
业务层:Spring框架,封装业务逻辑,逻辑复用
持久层:DAO模式、ORM模式,MyBatis,封装数据访问逻辑
域对象层:实现层和层之间的数据传递
1、创建一个Java Enterprise项目
<dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> <scope>provided</scope> </dependency> <dependency> <groupId>jstl</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency>
2、引入MyBatis框架
引入依赖
<dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.6</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.23</version> </dependency> <!--为了在控制台上显示所执行的SQL语句,方便开发调试 --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.17</version> </dependency>
定义核心配置文件 resources/mybatis-config.xml
<?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> <properties resource="database.properties"/> <settings> <setting name="logImpl" value="LOG4J"/> </settings> <environments default="yan"> <environment id="yan"> <transactionManager type="JDBC"/> <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> </configuration>
将数据库相关的配置信息分离到一个properties文件种
resources/database.properties
driver=com.mysql.cj.jdbc.Driver url=jdbc:mysql:///test?serverTimezone=UTC username=root password=123456
为了实现日志输出,resources/log4j.properties
# 全局日志配置 log4j.rootLogger=DEBUG, stdout # 控制台输出 log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%m%n
3、数据库设计
逻辑数据模型(ER图)—数据库选型—物理数据模型(SQL语句)
规范化法:创建模式—应用范式—反范式
create table t_users( id bigint primary key auto_increment, username varchar(20) not null unique, password varchar(20) not null, birth datetime default now(), sex boolean default 1, salary numeric(8,2) default 0 )engine=innodb default charset utf8;
建立和表对应的实体类,引入lombok
@Data public class UserBean implements Serializable { private Long id; private String username; private String password; private Date birth; private Boolean sex; private Double salary; }
定义相关的映射元文件 resources/com/yan/dao/UserBean-mapper.xml
为了简化映射元文件种引用类名编写的问题,在mybatis-config.xml中定义类型别名
<typeAliases> <typeAlias type="com.yan.entity.UserBean" alias="User"/> </typeAliases>
在映射元文件种如果要使用com.yan.entity.UserBean类型全名,可以使用别名User替代
<?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="com.yan.dao.UserMapper"> <resultMap id="userResultMapper" type="User" autoMapping="true"> <id column="id" property="id" javaType="long"/> <result column="username" property="username" javaType="string"/> <result column="password" property="password" javaType="string"/> <result column="birth" property="birth" jdbcType="DATE"/> </resultMap> </mapper>
定义对应的映射接口 com.yan.dao.UserMapper.java
public interface UserMapper { }