资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Spring怎么搭建web服务器-创新互联

I、springboot基本概念

从策划到设计制作,每一步都追求做到细腻,制作可持续发展的企业网站。为客户提供成都网站制作、做网站、外贸营销网站建设、网站策划、网页设计、国际域名空间、虚拟主机、网络营销、VI设计、 网站改版、漏洞修补等服务。为客户提供更好的一站式互联网解决方案,以客户的口碑塑造优易品牌,携手广大客户,共同发展进步。

1.受管Bean

Spring中那些组成应用的主体以及由Spring IoC容器所管理的对象被称之为bean;

Bean就是由Spring容器初始化、装配以及被管理的对象

2.控制反转IOC和依赖注入DI

IoC实现由容器控制程序之间的关系,而非传统实现中,由程序代码直接操控,控制权由应用代码中转到了外部容器,控制权的转移,是所谓控制反转;

创建被调用者实例的工作通常由Spring容器来完成,然后注入调用者,因此也称为依赖注入

依赖注入:接口注入、设置注入、构造器注入

3.AOP面向切面编程

AOP即Aspect-Oriented Programming, 面向切面编程是一种新的方法论,是一种技术,不是设计模式,是对传统OOP(Object-Oriented Programming,面向对象编程)的补充

AOP 的主要编程对象是切面(aspect), 而切面模块化横切关注点

II、搭建简单的web服务器(SSH)

1.添加jar包

整合maven,在pom.xml中添加

        
		
			org.springframework
			spring-jdbc
			${spring.version}
		
		
			org.springframework
			spring-context-support
			${spring.version}
		
		
			com.oracle
			ojdbc6
			1.5
			system
			${project.basedir}/src/main/webapp/WEB-INF/lib/ojdbc6.jar
		
	

2.整合Spring和web应用(在web.xml中添加)

        
		contextConfigLocation
		classpath:app*.xml
	
	
		org.springframework.web.context.ContextLoaderListener
	

3.整合struts2(在web.xml中添加)

        
		struts2
		org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
		
			struts.devMode
			true
		
	
	
		struts2
		/*
	

4.整合Hibernate

a.添加database.properties文件

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8

jdbc.username=root

jdbc.password=root

b.配置连接池(在applicationContext.xml中添加)

        
		

        c.配置SessionFactory(在applicationContext.xml中添加)

        
			
				
					org.hibernate.dialect.MySQL5InnoDBDialect
					true
					true
					update
					org.springframework.orm.hibernate5.SpringSessionContext
					(重点:使getCurrentSession不为null)
				
				

        d.配置事务管理SessionFactory(在applicationContext.xml中添加)

        配置注解事务有效
		

5.定义带注解的实体类

在src/com/wcg/entity包下添加实体类

        @Entity(name = "t_roles") //声明实体类,name是对应的表名称
	@Cache(usage=CacheConcurrencyStrategy.READ_ONLY) //配置二级缓存的并发访问策略,read_only/read_write  NONSTRICT_READ_WRITE  TRANSACTIONAL
	public class RoleBean implements Serializable {
		private static final long serialVersionUID = -7132291370667604346L;
		@Id	//声明主键
		@GeneratedValue(strategy = GenerationType.IDENTITY)
		private Long id;
		@Column(length = 32, unique = true, nullable = false)//定义类说明,length字串长度unique唯一性约束,nullable非空约束
		private String name;
		@Basic(fetch = FetchType.LAZY)//针对属性的加载策略,需要支持
		@Column(length = 200)
		private String descn;
		@OneToMany(mappedBy = "role", cascade = CascadeType.ALL)//说明一对多关联,mapedby表示由对方负责维护两者之间的关联关系,cascade级联策略
		private Set users = new HashSet<>();	

6.定义DAO接口--IBaseDao

在src/com/wcg/dao包下添加DAO接口

        public interface IBaseDao {
		T save(T record);
		T delete(T record);
		T load(ID id);
		T update(T record);
		List selectByExample(T record, int... pages);
		int selectByExampleRowsNum(T record);	//查询满足条件的一共有多少行
	}
	//--------------------------
	public interface IRoleDao extends IBaseDao {

	}
	//--------------------------
	public class BaseDaoImpl implements IBaseDao {
	@Autowired
	private SessionFactory sessionFactory;
	private Class recordClass;
	
	@SuppressWarnings("unchecked")
	public BaseDaoImpl() {
		this.recordClass = (Class) (((ParameterizedType) getClass().getGenericSuperclass())
				.getActualTypeArguments()[0]);
	}
	
	protected Session getSession() {
		return sessionFactory.getCurrentSession();
	}
	
	@Override
	public T save(T record) {
		this.getSession().save(record);
		return record;
	}

	@Override
	public T delete(T record) {
		this.getSession().delete(record);
		return record;
	}

	@Override
	public T load(ID id) {
		return this.getSession().get(recordClass, id); 
	}

	@Override
	public T update(T record) {
		this.getSession().update(record);
		return record;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List selectByExample(T record, int... pages) {
		Criteria criteria = this.getSession().createCriteria(recordClass);
		if(record != null)
			criteria.add(Example.create(record));
		if(pages != null && pages.length > 0)
			criteria.setFirstResult(pages[0]);
		if(pages != null && pages.length > 1)
			criteria.setMaxResults(pages[1]);
		return criteria.list();
	}
	//--------------------------
	@Repository("roleDao")//声明DAO类型的受管bean,其中的value就是名称,类似于的id
	public class RoleDaoImpl extends BaseDaoImpl implements IRoleDao {

	}

7.定义业务

在src/com/wcg/biz包下添加Server接口

        public interface IRoleServ {
		void create(RoleBean role);
	}
	//--------------------------
	@Service  //定义业务类型的受管bean,如果不定义名称,则默认名称就是类名称,首字母小写
	@Transactional(readOnly = true, propagation = Propagation.SUPPORTS) //声明事务,readOnly只读事务propagation事务传播特性
	public class RoleServImpl implements IRoleServ {
		@Resource(name = "roleDao")
		private IRoleDao roleDao;

		@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
		public void create(RoleBean role) {
			roleDao.save(role);
		}

8.定义Action

在src/com/wcg/action包下添加action类

        @Controller  //定义控制器类型的受管bean
	@Scope("prototype") //定义scope=prototype以保证Action的多实例
	@Namespace("/")  //Struts2的注解,表示名空间
	@ParentPackage("struts-default") //Struts2的注解,定义继承的包名称
	public class UserAction extends ActionSupport implements ModelDriven, SessionAware {
		private UserBean user = new UserBean();
		private Map sessionAttribute;

		@Autowired
		private IUserServ userv;

		@Action(value = "tologin", results = { @Result(name = "success", location = "/WEB-INF/content/user/login.jsp") })
		public String toLogin() throws Exception {
			return SUCCESS;
		}

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章标题:Spring怎么搭建web服务器-创新互联
分享路径:http://cdkjz.cn/article/djhjpj.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220