spring+hibernate初探(电影查询项目)

一、spring框架简介

Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson创建。简单来说,Spring是一个分层的JavaSE/EE full-stack(一站式) 轻量级开源框架。

二、spring配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <context:component-scan base-package="com"/>

    <bean name="student" class="com.ly.javabean.Student">
        <property name="className" value="3年5班"/>
    </bean>
    <bean name="student8" class="com.ly.javabean.Student">
        <property name="className" value="3年4班"/>
    </bean>
    <!-- <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
         <property name=""
     </bean>-->

    <bean name="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
        <property name="configLocation" value="classpath:hibernate.cfg.xml"/>
    </bean>

    <!--<context:component-scan base-package="com" />-->
    <bean name="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>

    <bean name="dao" class="com.ly.dao.daoImpl.SysDaoImpl">
        <property name="hibernateTemplate" ref="hibernateTemplate"/>
    </bean>


</beans>

context:component-scan:使用构造型(stereotype)注解所标注的类,如@Component(组件),@Service(服务),@Controller(控制器),@Repository(数据仓库)。

三、spring简单使用

@RunWith(SpringJUnit4ClassRunner.class)
@Component
public class SpringTest {

    @Autowired
    private Sysdao dao;


    @Test
    public void demo1(){
        /*ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        Sysdao dao = applicationContext.getBean("dao",Sysdao.class);*/

        List apkInfo = dao.find("from ApkInfo", null);

        System.out.println(Arrays.toString(apkInfo.toArray()));
    }
}

由于有了spring的管理,对象的获取可以直接有spring管理,不用直接new一个对象。这里可以使用配置文件的格式获取,也就是上述注释的部分来获取一个对象,也可使用注解的方式获取,但是在配置文件中必须加上context:component-scan才可使用。

四、log4j

可引入log4j来记录日志。

# Global logging configuration 开发时候建议使用 debug
log4j.rootLogger=info, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS}%5p [%t] - %m%n

五、controller、service、dao的搭建

 dao和service只是简单的查询,不在展示。

controller:

@WebServlet(name = "com.ly.controller.ShowViewServlet", urlPatterns = "/show/aa")

public class ShowViewServlet extends HttpServlet {

    private Logger log = LoggerFactory.getLogger(ShowViewServlet.class);

    public ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        doGet(request, response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Myservice service = (Myservice) context.getBean("myserviceImpl");
        response.setContentType("text/html;charset=utf-8");
        String param = request.getParameter("param");
        param = "%" + param + "%";
        String hql = "select a from FilmBtsNew a where a.name like '" + param + "'";
        List list = service.find(hql);
        log.info("参数:{}", param);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("person", list);
        response.getWriter().print(jsonObject);

    }
}

结果如下:

现在只是简单的将json数据上传至web,下一步学习了简单的前端就可以实现一个电影主页了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值