Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

Monday, June 1, 2015

spring Mvc 之helloworld

eclipse 安装spring开发包:https://spring.io/tools/sts/all
并安装
下载日志记录器: http://mvnrepository.com/artifact/commons-logging/commons-logging
下载spring的包:http://repo.springsource.org/libs-release-local/org/springframework/spring/4.0.0.RELEASE/spring-framework-4.0.0.RELEASE-dist.zip

新建HelloWorld class
package edu.spring.beans;

public class HelloWorld {
private String name;
public void setNamex(String name){
this.name=name;
}
public void hello(){
System.out.println("hello "+name);
}

}

新建配置文件appContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="helloworld" class="edu.spring.beans.HelloWorld">
<property name="namex" value="Spring"></property></bean>
</beans>

新建Main class

public class Main {

public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("appContext.xml");
HelloWorld helloworld=(HelloWorld) ctx.getBean("helloworld");
helloworld.hello();
}

}


结果:

hello Spring

Sunday, February 22, 2015

ASP.NET MVC+Entity Framework 4.1访问数据库

Entity Framework 4.1支持代码优先(code first)编程模式:即可以先创建模型类,然后通过配置在EF4.1下动态生成数据库。
下面演示两种情形:
1、代码优先模式下,asp.net mvc数据访问
2、传统模式,先创建数据库和表,配置连接字符串,再生成模型

第一种情况的步骤:
(1)使用空模板,创建ASP.NET MVC3.0(或4.0)项目,假定项目名:MVC_Student
注意:创建完项目后,项目会自动引用EF4.1
(2)在Model文件夹下,创建数据库上下文类:StuDBContext
public class StuDBContext:DbContext
    {
        public StuDBContext()
            : base("DataConn")
        {
        }
        public DbSet<StudentInfo> Students { get; set; }
    }
(3)创建域模型:StudentInfo
public class StudentInfo
    {
        public int ID { get; set; }
        public string StuNO { get; set; }
        public string StuName { get; set; }
        public string StuPhoto { get; set; }
        public DateTime StuBirthday { get; set; }
        public string StuAddress { get; set; }
    }
(4)在web.config中配置连接字符串(也可以不配置,EF自动检查并使用SQL SERVER EXPRESS,此处我们指定服务器和数据库)
<connectionStrings>
  <!--<add name="StuDBContext" connectionString="server=(local);database=MyStudent;uid=(登录账户);pwd=(登录密码)" providerName="System.Data.SqlClient"/>-->
  <add name="DataConn" connectionString="server=(local);database=MyStudent;uid=(登录账户);pwd=(登录密码)" providerName="System.Data.SqlClient"/>
 </connectionStrings>
(5)生成项目,为第(6)步服务
(6)右击“Controllers"文件夹,选择”添加控制器“,如图:
单击确定后,会在Controllers文件夹下生成一个StudentController类,而且在Views文件夹下生成Student子文件夹,其中包含5个.cshtml文件,如图:
(7)修改Global.asax.cs的默认路由:
routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Student", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );
(8)最后,单击”调试“菜单,选择”启动调试“,或直接按F5.会看到如下效果:
(9)可以单击”Create New"超链接,向数据库添加一条记录
(10)此时可以打开数据库服务器,会发现自动创建了MyStudent的数据库(对应连接字符串中的数据库)和StudentInfoes表(是模型类名称的复数形式,表中的各字段分别对应模型类中的属性,此处要特别注意:ID属性会自动对应表中的自增长主键列。

以上方法需要注意的地方:
(1)web.config中连接字符串,providerName要提供,否则报错。
(2)模型的ID属性是固定的, 要不就要用元数据声明,EF4.1会自动将之映射为表的主键(自增长)。
(3)数据库实体上下文名称一般与连接字符串name属性的值相同,但本文中不同(数据库实体上下文是StuDBContext,连接字符串名称:DataConn),如果相同,那么实体上下文类可以不提供构造函数。如果不相同,如本例中,可以为实体上下文添加构造函数,并调用父类构造函数,在base()中传递与实体上下文类名不同的连接字符串名称(本例中时DataConn,如步骤(2)红色标注)
 ________________________________________________________________________________________________________________________________________
下面演示第2种情形:先创建数据库模式,然后生成模型
(1)在SQL SERVER 2005/2008服务器上创建数据库MyStudent,并添加一张表StudentInfoes(也可以在VS中服务器资源管理器中操作)
表的结构如下;
(2)创建ASP.NET MVC3/4项目(使用空模板)
(3)在Models文件夹中添加实体上下文类:StuDBContext
public class StuDBContext:DbContext
    {
        public StuDBContext()
            : base("DataConn")
        {
        }
        public DbSet<StudentInfo> Students { get; set; }
    }
(4)在Models文件夹中添加实体类:StudentInfo
public class StudentInfo
    {
        public int ID { get; set; }
        public string StuNO { get; set; }
        public string StuName { get; set; }
        public string StuPhoto { get; set; }
        public DateTime StuBirthday { get; set; }
        public string StuAddress { get; set; }
    }
(5)在web.config中配置连接字符串:
<connectionStrings>
  <!--<add name="StuDBContext" connectionString="server=(local);database=MyStudent;uid=(登录账户);pwd=(登录密码)" providerName="System.Data.SqlClient"/>-->
  <add name="DataConn" connectionString="server=(local);database=MyStudent;uid=(登录账户);pwd=(登录密码)" providerName="System.Data.SqlClient"/>
 </connectionStrings>
(6)生成项目解决方案,为第(7)步服务
(7)在Controllers文件夹下添加StudentController类,如图:
单击添加后,会创建与第一种情形同样的项目文件结构。如上面所示。
(8)修改路由:
routes.MapRoute(
                "Default", // 路由名称
                "{controller}/{action}/{id}", // 带有参数的 URL
                new { controller = "Student", action = "Index", id = UrlParameter.Optional } // 参数默认值
            );
(9)启动调试,预览效果如下:


对比两种方式,有如下区别:自动生成数据库表的字段类型可能与自定义的不一致。

无论是通过代码生成数据库,还是先创建数据库,再创建模型,都需要了解他们之间的映射关系(即EF作为ORM框架的作用),你会发现EF4.1作为ORM框架,遵循很多约定(Convention),而ASP.NET MVC推崇的很重要的一条原则就是:约定先于配置,可谓不谋而合。

Saturday, February 7, 2015

asp.net MVC 对数据库进行操作

using EntityFramework.Extensions;
//外界扩展库+ 需要在PM里 Install-Package EntityFramework.Extended


//删除时间超过12小时的信息
            db.channel.Delete(item => DbFunctions.DiffHours(item.Date,newTime) > 12);