1. 准备数据库环境
# 新建用户表
CREATE TABLE IF NOT EXISTS `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`username` VARCHAR(32) NOT NULL COMMENT '用户名称',
`birthday` DATETIME DEFAULT NULL COMMENT '生日',
`sex` CHAR(1) DEFAULT NULL COMMENT '性别',
`address` VARCHAR(256) DEFAULT NULL COMMENT '地址',
`password` VARCHAR(100) DEFAULT NULL COMMENT '密码',
CONSTRAINT pk_user PRIMARY KEY (`id`)
);
# 添加用户数据
INSERT INTO
`user`(`id`,`username`,`birthday`,`sex`,`address`,`password`)
VALUES
(1,'周瑜','2020-02-27 17:47:08','男','吴国','123456'),
(2,'大乔','2020-02-27 15:09:37','女','吴国','123456'),
(3,'小乔','2020-02-27 11:34:34','女','吴国','123456'),
(4,'陆逊','2020-02-27 12:04:06','男','吴国','123456'),
(5,'孙策','2020-02-27 17:37:26','男','东吴','123456'),
(6,'孙权','2020-02-27 11:44:00','男','吴国','123456');
2. 创建maven工程
3. 项目依赖
< dependencies>
< dependency>
< groupId> mysql</ groupId>
< artifactId> mysql-connector-java</ artifactId>
< version> 5.1.49</ version>
</ dependency>
< dependency>
< groupId> org.projectlombok</ groupId>
< artifactId> lombok</ artifactId>
< version> 1.18.12</ version>
</ dependency>
</ dependencies>
4. 项目工程结构
5. Bean工厂配置文件
# Bean配置文件
# 用户持久层Bean
userDao=zw.spring.dao.impl.UserDaoImpl
# 用户业务层Bean
userService=zw.spring.service.impl.UserServiceImpl
6. Bean工厂工具类
package zw. spring. util;
import java. io. IOException;
import java. io. InputStream;
import java. util. HashMap;
import java. util. Map;
import java. util. Properties;
public class BeanFactoryUtil
{
private static final String CONFIG_FILE_NAME = "beanfactory_config.properties" ;
private static Properties properties;
private static Map< String, Object> container;
static
{
InputStream is = null;
try
{
properties = new Properties ( ) ;
is = BeanFactoryUtil. class . getClassLoader ( ) . getResourceAsStream ( CONFIG_FILE_NAME) ;
properties. load ( is) ;
container = new HashMap < > ( ) ;
}
catch ( IOException e)
{
e. printStackTrace ( ) ;
}
finally
{
try
{
if ( is != null)
{
is. close ( ) ;
}
}
catch ( IOException e)
{
e. printStackTrace ( ) ;
}
}
}
public static Object createBean ( String beanName)
{
Object object = container. get ( beanName) ;
if ( object == null)
{
try
{
Class classObject = Class. forName ( properties. getProperty ( beanName) ) ;
object = classObject. newInstance ( ) ;
container. put ( beanName, object) ;
}
catch ( ClassNotFoundException e)
{
e. printStackTrace ( ) ;
}
catch ( IllegalAccessException e)
{
e. printStackTrace ( ) ;
}
catch ( InstantiationException e)
{
e. printStackTrace ( ) ;
}
}
return object;
}
}
7. 数据库工具类
package zw. spring. util;
import java. sql. *;
public class DBUtil
{
private static final String JDBC_MYSQL_DRIVER = "com.jdbc.mysql.Driver" ;
private static final String JDBC_MYSQL_URL = "jdbc:mysql://127.0.0.1:3306/mybatis?useSSL=false" ;
private static final String JDBC_MYSQL_USERNAME = "root" ;
private static final String JDBC_MYSQL_PASSWORD = "root" ;
static
{
try
{
Class. forName ( JDBC_MYSQL_DRIVER) ;
}
catch ( ClassNotFoundException e)
{
e. printStackTrace ( ) ;
}
}
public static Connection getConnection ( )
{
Connection connection = null;
try
{
connection = DriverManager. getConnection ( JDBC_MYSQL_URL, JDBC_MYSQL_USERNAME, JDBC_MYSQL_PASSWORD) ;
}
catch ( SQLException throwables)
{
throwables. printStackTrace ( ) ;
}
return connection;
}
public static void closeConnection ( Connection connection, PreparedStatement preparedStatement, ResultSet resultSet)
{
try
{
if ( resultSet != null)
{
resultSet. close ( ) ;
}
if ( preparedStatement != null)
{
preparedStatement. close ( ) ;
}
if ( connection != null)
{
connection. close ( ) ;
}
}
catch ( SQLException throwables)
{
throwables. printStackTrace ( ) ;
}
}
}
8. 用户实体类
package zw. spring. domain;
import lombok. Data;
import java. io. Serializable;
import java. util. Date;
@Data
public class UserDO implements Serializable
{
private Integer id;
private String username;
private String password;
private Date birthday;
private String sex;
private String address;
}
9. 用户持久层接口
package zw. spring. dao;
import zw. spring. domain. UserDO;
import java. util. List;
public interface UserDao
{
List< UserDO> listUserInformations ( ) ;
}
10. 用户持久层实现类
package zw. spring. dao. impl;
import zw. spring. dao. UserDao;
import zw. spring. domain. UserDO;
import zw. spring. util. DBUtil;
import java. sql. Connection;
import java. sql. PreparedStatement;
import java. sql. ResultSet;
import java. sql. SQLException;
import java. util. ArrayList;
import java. util. Date;
import java. util. List;
public class UserDaoImpl implements UserDao
{
@Override
public List< UserDO> listUserInformations ( )
{
List< UserDO> result = new ArrayList < > ( ) ;
Connection connection = null;
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
try
{
String sql = "select * from user" ;
connection = DBUtil. getConnection ( ) ;
preparedStatement = connection. prepareStatement ( sql) ;
resultSet = preparedStatement. executeQuery ( ) ;
while ( resultSet. next ( ) )
{
Integer id = resultSet. getInt ( "id" ) ;
String username = resultSet. getString ( "username" ) ;
String password = resultSet. getString ( "password" ) ;
Date birthday = resultSet. getDate ( "birthday" ) ;
String sex = resultSet. getString ( "sex" ) ;
String address = resultSet. getString ( "address" ) ;
UserDO user = new UserDO ( ) ;
user. setId ( id) ;
user. setUsername ( username) ;
user. setPassword ( password) ;
user. setBirthday ( birthday) ;
user. setSex ( sex) ;
user. setAddress ( address) ;
result. add ( user) ;
}
}
catch ( SQLException throwables)
{
throwables. printStackTrace ( ) ;
}
finally
{
DBUtil. closeConnection ( connection, preparedStatement, resultSet) ;
}
return result;
}
}
11. 用户业务层接口
package zw. spring. service;
import zw. spring. domain. UserDO;
import java. util. List;
public interface UserService
{
List< UserDO> lisetUserInformations ( ) ;
}
12. 用户业务层实现类
package zw. spring. service. impl;
import zw. spring. dao. UserDao;
import zw. spring. domain. UserDO;
import zw. spring. service. UserService;
import zw. spring. util. BeanFactoryUtil;
import java. util. List;
public class UserServiceImpl implements UserService
{
private UserDao userDao = ( UserDao) BeanFactoryUtil. createBean ( "userDao" ) ;
@Override
public List< UserDO> lisetUserInformations ( )
{
return userDao. listUserInformations ( ) ;
}
}
13. 用户表现层控制器
package zw. spring. controller;
import zw. spring. domain. UserDO;
import zw. spring. service. UserService;
import zw. spring. util. BeanFactoryUtil;
import java. util. List;
public class UserController
{
public static void main ( String[ ] args)
{
UserService userService = ( UserService) BeanFactoryUtil. createBean ( "userService" ) ;
List< UserDO> users = userService. lisetUserInformations ( ) ;
for ( UserDO user : users)
{
System. out. println ( "user = " + user) ;
}
}
}
14. 运行测试
15. 单例工厂测试类
package zw. spring. controller;
import zw. spring. service. UserService;
import zw. spring. util. BeanFactoryUtil;
public class TestController
{
public static void main ( String[ ] args)
{
for ( int i = 0 ; i < 10 ; i++ )
{
UserService userService = ( UserService) BeanFactoryUtil. createBean ( "userService" ) ;
System. out. println ( "userService = " + userService) ;
}
}
}
16. 运行测试