宁德时代网站哪个公司做的,合肥公司注册,115做网站,上海诚杰华建设工程咨询有限公司网站基本准备
创建Dynamic Web Project
引入相关jar包 Spring框架相关jar包 MyBatis连接Spring相关jar包 连接MySQL驱动包 JSTL标签库包
添加db.properties文件#xff0c;该属性文件配置连接数据库相关信息
drivercom.mysql.jdbc.Driver
urljdbc:mysql://localhost:3306/myba…基本准备
创建Dynamic Web Project
引入相关jar包 Spring框架相关jar包 MyBatis连接Spring相关jar包 连接MySQL驱动包 JSTL标签库包
添加db.properties文件该属性文件配置连接数据库相关信息
drivercom.mysql.jdbc.Driver
urljdbc:mysql://localhost:3306/mybatis?characterEncodingutf-8
userroot
password添加Spring配置文件在src中新增applicationContext.xml并引入该属性文件
!-- 导入数据库连接信息的属性文件 --
context:property-placeholder locationclasspath:db.properties/在Spring配置文件中添加DataSource的配置PooledDataSource为MyBatis实现的数据库连接池
!-- 配置数据源 --
bean iddataSource classorg.apache.ibatis.datasource.pooled.PooledDataSource
property namedriver value${driver}/property
property nameusername value${user}/property
property namepassword value${password}/property
property nameurl value${url}/property
/bean在Spring配置文件中添加SqlSessionFactoryBean用来创建SqlSessionFactory对象 configLocation用于指定MyBatis的mybatis.xml配置文件的路径 dataSource用于配置数据源该属性为必选项可以直接引用已经配置好的dataSource数据库连接池 mapperLocations扫描XML映射文件的路径
bean idsqlSessionFactory classorg.mybatis.spring.SqlSessionFactoryBean!-- 添加mybatis主配置文件的位置 --property nameconfigLocation valueclasspath:mybatis.xml/!-- 需要一个数据源 --property namedataSource refdataSource/property!-- 设置映射文件的位置 --property namemapperLocationsarrayvalueclasspath:net/onest/server/dao/*.xml/value/array/property
/bean在Spring配置文件中添加MapperScannerConfigurer自动扫描所有的Mapper接口
basePackage用于配置基本的包路径
bean classorg.mybatis.spring.mapper.MapperScannerConfigurerproperty namebasePackage valuenet.onest.server.dao/
/bean按照SSH集成的web.xml添加Spring和SpringMVC相关配置
创建实体类
创建数据库表
创建index.jsp 开发Mapper层Dao层
Mapper层也就是常说的数据访问层Dao层。根据配置文件中配置的自动扫描接口的包名创建映射接口和XML映射文件
public interface UserMapper {public ListUser findAllUsers();public int saveUser(User u);
}mapper namespacenet.onest.server.dao.UserMapperresultMap typenet.onest.server.entity.User iduserMapid columnid propertyid/result columnuser_name propertyuserName/result columnpassword propertypassword//resultMapselect idfindAllUsers resultMapuserMapselect * from user/selectinsert idsaveUserinsert into user(user_name,password) values(#{userName},#{password})/insert
/mapper开发业务层Service层
添加Service层的接口和实现类
public interface UserService {public ListUser findAllUsers();public int saveUser(User u);
}Service
public class UserServiceImpl implements UserService{Autowiredprivate UserMapper userMapper;Overridepublic ListUser findAllUsers() {return userMapper.findAllUsers();}Overridepublic int saveUser(User u) {return userMapper.saveUser(u);}
}Service的实现类需要添加Service注解由于在Spring配置文件中配置了自动扫描Service实现类所在的包所以Spring在初始化时就会扫描到添加了Service注解的类
由于配置了自动扫描Mapper接口所以在Service层可以使用AutoWired注解自动注入Mapper 开发控制层Controller层
RequestMapping(/user)
Controller
public class UserController {Autowiredprivate UserService userService;RequestMapping(/userList)public ModelAndView getUsers() {ModelAndView mv new ModelAndView(userList);ListUser users userService.findAllUsers();mv.addObject(users, users);return mv;}……
}RequestMapping(/addUser)
public ModelAndView addUser() {ModelAndView mv new ModelAndView(addUser);User u new User();mv.addObject(user, u);return mv;
}
RequestMapping(/saveUser)
public ModelAndView saveUser(User u) {ModelAndView mv new ModelAndView();userService.saveUser(u);mv.setViewName(redirect:/user/userList);return mv;
}开发视图层View层
根据在SpringMVC配置文件中的视图配置需要在WebContent/WEB-INF中新建jsp目录来存放所有的jsp文件
bean idviewResolver class
org.springframework.web.servlet.view.InternalResourceViewResolverproperty nameprefix value/WEB-INF/jsp//property namesuffix value.jsp/
/bean