本文主要是介绍ssm_ums整合步骤,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
ssm_ums整合步骤
1.创建Maven webapp项目
2.导入jar包和插件
3.拷贝UMS页面
4.创建java和resources文件夹
5.初始化数据库和表
CREATE TABLE t_user (uid INT(11) PRIMARY KEY NOT NULL AUTO_INCREMENT,login_name VARCHAR(50) DEFAULT NULL,login_pwd VARCHAR(32) DEFAULT NULL,age INT(11) DEFAULT NULL,birthday DATE DEFAULT NULL,sex VARCHAR(1) DEFAULT NULL,education VARCHAR(50),telephone VARCHAR(11),interest VARCHAR(100),remark VARCHAR(200)
)insert into `t_user`(`uid`,`login_name`,`login_pwd`,`age`,`birthday`,`sex`,`education`,`telephone`,`interest`,`remark`) values (1,'jack','1234',18,'1996-11-11','男',NULL,NULL,NULL,NULL),(2,'rose','1234',21,'1993-11-11','女',NULL,NULL,NULL,NULL),(3,'tom','1234',23,'1996-12-24','男',NULL,NULL,NULL,NULL);
6.创建db.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/表名
jdbc.username=用户名
jdbc.password=密码
7.项目目录
8.创建javaBean
@Table(name="t_user")
public class User {
@Id
private Integer uid;
private String loginName;
private String loginPwd;
private Integer age;
private String sex;
private String birthday;
private String education;
private String telephone;
private String interest; //爱好:对应数据库(内容:A,B,C)
private String[] interestArr; //爱好:对应页面表单
private String remark;爱好需要特殊处理
public String getInterest() {if(interestArr != null){interest = Arrays.toString(interestArr); //[A,B,C]interest = interest.substring(1, interest.length() - 1); //A,B,C}return interest;
}
public void setInterest(String interest) {this.interest = interest;if(interest != null){interestArr = interest.split(", ");}
}
9.编写mapper接口
public interface UserMapper extends Mapper<User> {
}
10.编写service接口
public interface UserService {public List<User> findAll();}
11.编写service实现类
@Service(value = "userServiceImpl")
@Transactional
public class UserServiceImpl implements UserService {@Resourceprivate UserMapper userMapper;@Overridepublic List<User> findAll() {List<User> users = userMapper.selectAll();return users;}
}
12.编写controller实现类
@Controller
public class UserController {@Autowiredprivate UserService userService;@RequestMapping("/findAllUsers.action")public String findAll(Model model){List<User> userList = userService.findAll();model.addAttribute("userList", userList);return "/user/list.jsp";}
}
13.编写jsp页面
14.编写spring配置类
@Configuration
//要扫描的包
@ComponentScan(basePackages = {"com.czxy"})
//开启事务支持
@EnableTransactionManagement
//读取properties配置文件
@PropertySource(value = "classpath:db.properties")
public class SpringConfig {//4.2.4 读取properties的固定代码@Beanpublic static PropertySourcesPlaceholderConfigurer create(){return new PropertySourcesPlaceholderConfigurer();}//读取数据库中的相关配置@Value("${jdbc.driver}")private String driverClass;@Value("${jdbc.url}")private String url;@Value("${jdbc.username}")private String username;@Value("${jdbc.password}")private String password;//设置德鲁伊连接池@Beanpublic DataSource dataSource(){DruidDataSource dataSource = new DruidDataSource();dataSource.setPassword(password);dataSource.setUsername(username);dataSource.setUrl(url);dataSource.setDriverClassName(driverClass);return dataSource;}//开启事务管理器@Bean@Resourcepublic DataSourceTransactionManager txManager(DataSource dataSource){return new DataSourceTransactionManager(dataSource);}
}
15.编写mybatis配置类
@Configuration
public class MybatisConfig {//获取sessionFactory对象,@Bean@Resourcepublic SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {//1.设置SQL// 通过工厂bean创建对象,对吼需要调用 getObject获取具体对象SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();// 设置数据源factoryBean.setDataSource(dataSource);// 设置别名 要扫描的包factoryBean.setTypeAliasesPackage("com.czxy.domain");// 全局驼峰映射org.apache.ibatis.session.Configuration config = new org.apache.ibatis.session.Configuration();config.setMapUnderscoreToCamelCase(true);factoryBean.setConfiguration(config);//2.插件配置 分页PageHelper pageHelper = new PageHelper();Properties pro = new Properties();pro.setProperty("dialect", "mysql");pro.setProperty("rowBoundsWithCount", "true");pageHelper.setProperties(pro);factoryBean.setPlugins(new Interceptor[]{pageHelper});//3. 通过工厂bean 获取 SQLSessionFactoryreturn factoryBean.getObject();}/*** 设置要扫描的 mapper包 将创建好的XXXMapper对象存放到IOC容器中* @return*/@Beanpublic MapperScannerConfigurer mapperScanner(){MapperScannerConfigurer configurer = new MapperScannerConfigurer();configurer.setBasePackage("com.czxy.dao");return configurer;}
}
16.编写springMVC配置类
//声明为配置类
@Configuration
//设置扫描 controller
@ComponentScan("com.czxy.controller")
public class SpringMvcConfig {
}
17.编写web启动配置类配置类
public class WebInitializer implements WebApplicationInitializer {@Overridepublic void onStartup(ServletContext servletContext) throws ServletException {//1.初始化 Spring 容器AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();applicationContext.register(SpringMvcConfig.class);applicationContext.register(SpringConfig.class);applicationContext.register(MybatisConfig.class);applicationContext.setServletContext(servletContext);//2.设置核心控制器ServletRegistration.Dynamic ds = servletContext.addServlet("springmvc", new DispatcherServlet(applicationContext));ds.addMapping("*.action");ds.setLoadOnStartup(2);//3 post乱码配置 FilterRegistration.Dynamic encodingFilter = servletContext.addFilter("CharacterEncodingFilter", new CharacterEncodingFilter("UTF-8"));encodingFilter.addMappingForUrlPatterns(null, true, "/*");}
}
18.测试
这篇关于ssm_ums整合步骤的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!